Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA—使用getter和setter为空值执行条件if语句_Java_If Statement_Null_This_Getter Setter - Fatal编程技术网

JAVA—使用getter和setter为空值执行条件if语句

JAVA—使用getter和setter为空值执行条件if语句,java,if-statement,null,this,getter-setter,Java,If Statement,Null,This,Getter Setter,我在使用getter和setter从另一个类返回并替换null值时遇到了一个问题,我想知道我的错误之处 getter和setter的配置: public class CarClass { private String make; private String model; private int yearOfMake; public void setMake(String make){ if (make.isEmpty()){

我在使用getter和setter从另一个类返回并替换null值时遇到了一个问题,我想知道我的错误之处

getter和setter的配置:

public class CarClass {

    private String make;
    private String model;
    private int yearOfMake;

    public void setMake(String make){
        if (make.isEmpty()){
            this.make = "";
            System.out.println("Error please input value");
        } else {
            System.out.println("Hello");
            this.make = make; 
        }
    }

    public String getMake(){
        return make;
    }

    public String getModel() {

        return model;
    }

    public void setModel(String model) {
        if (model.isEmpty()){
            this.model = "";
            System.out.println("Error, you have not entered a valid model");
        } else {
            this.model = model;
        }
    }

    public int getYearOfMake() {
        return yearOfMake;
    }

    public void setYearOfMake(int yearOfMake) {
        if (yearOfMake > 1900)
            this.yearOfMake = yearOfMake;
        else {
            this.yearOfMake = yearOfMake;
            System.out.println("Year of make is invalid");
        }
    }
}
用于上述内容的执行脚本:

public class GetterSetter {

    public static void main(String[] args) {
        CarClassTwo bmw = new CarClassTwo();
        bmw.setMake();
        System.out.println(bmw.getMake());
        CarClass benz = new CarClass();
        benz.setModel("C300");
        System.out.println(benz.getModel());
        benz.setYearOfMake(1999);
        System.out.println(benz.getYearOfMake());
        System.out.println("!!!!!!!!");
        benz.setModel("C300");
        System.out.println(benz.getModel());
        benz.setYearOfMake(1800);
        System.out.println(benz.getYearOfMake());
        System.out.println(bmw.getMake());
        System.out.println(bmw.getModel());
    }
}

如果您能帮助我理解我所做的错误,我们将不胜感激。

执行以下代码时:

CarClassTwo bmw = new CarClassTwo();
bmw.setMake();
当时的价值观

private String make;
private String model;
默认情况下初始化为null

所以当bmw.setMake;执行时,如果make.isEmpty{


这类似于null.isEmpty->which将抛出NullPointerException。

您到底遇到了什么问题?NullPointerException?在这段代码中,您没有检查任何内容是否为null。您在几个地方调用isEmpty,但如果变量为null,则会抛出异常。如果您想检查例如make是否为null,如果make==null,则应该编写。但是,如果setMake方法需要参数,则不能在没有参数的情况下调用setMake。不传递参数与该参数为null是不同的。啊,我想我理解了你所说的。我想做的是检查默认变量是否为null,只是显示一条错误消息,告诉你必须输入er something我想生成一条错误消息,如果我的默认值为空,或者在这个实例中为'null'@ras,没有根据您的代码定义任何默认值。如果要分配默认值,请在开始时自己分配它们。如下所示:``private String make=default\u make;private String model=default\u model;private int yearOfMake=0```