为什么我得到错误这个变量在java类中是空的

3phpmpom  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(173)

我正在尝试用java编写一个小代码。方法gettotalcost()出错。你能检查一下我的密码并指出我犯了什么错误吗。
我正在做一个班级汉堡。将3个变量传递给consuctor。那我就选择在汉堡里加西红柿或菠菜。在gettotalcost()中,我试图打印mylettuce的值。 Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.amitsuneja.Lettuce.getCostOfLettuce()" because "this.myLettuce" is null at com.amitsuneja.Hamburger.getTotalCost(Hamburger.java:77) at com.amitsuneja.Main.main(Main.java:8) 这是我的汉堡包.java

public class Hamburger {

private String breadType;
private boolean isMeat;
private double priceOfBurger;
private Lettuce myLettuce;
private boolean addLettuce;
private Tomato myTomato;
private boolean addTomato;
private Carrot myCarrot;
private boolean addCarrot;
private Spinach mySpinach;
private boolean addSpinach;
private double toalCost;

Scanner myScanner = new Scanner(System.in);

public Hamburger(String breadType, boolean isMeat, double priceOfBurger) {
    this.breadType = breadType;
    this.isMeat = isMeat;
    this.priceOfBurger = priceOfBurger;
    System.out.println("Current cost of burger is: " + this.priceOfBurger);

    System.out.println(" Would like to add some Lettuce?");
    addLettuce = myScanner.nextBoolean();
    if (addLettuce){
        Lettuce myLettuce1 = new Lettuce(true);
    }else{
        Lettuce myLettuce1 = new Lettuce(false);
        System.out.println("I am in Lettuce: " + myLettuce.getCostOfLettuce() + myLettuce.isHaveLettuce());
    }
    myScanner.nextLine();

    System.out.println(" Would like to add some Tomato?");
    addTomato = myScanner.nextBoolean();
    if (addTomato){
        Tomato myTomato = new Tomato(true);
        }else{
        Tomato myTomato = new Tomato(false);
    }
    myScanner.nextLine();

    System.out.println(" Would like to add some carrot?");
    addCarrot = myScanner.nextBoolean();
    if (addCarrot){
        Carrot myCarrot = new Carrot(true);
    }else{
        Carrot myCarrot = new Carrot(false);
    }
    myScanner.nextLine();

    System.out.println(" Would like to add some Spinach?");
    addSpinach = myScanner.nextBoolean();
    if (addSpinach){
        Spinach mySpinach = new Spinach(true);
    }else{
        Spinach mySpinach = new Spinach(false);
    }
    myScanner.nextLine();
    myScanner.close();

}

public double getPriceOfBurger() {
    return priceOfBurger;
}

public void getTotalCost(){
    System.out.println("I am Here....................price = " + priceOfBurger);
    System.out.println("I am in Lettuce: " + myLettuce.getCostOfLettuce() + myLettuce.isHaveLettuce());

}

这是我的生菜类套餐com.amitsuneja;
公共级莴苣{

private boolean haveLettuce;
private double costOfLettuce;

public Lettuce(boolean haveLettuce) {
    this.haveLettuce = haveLettuce;
    this.costOfLettuce =2d;
}

public boolean isHaveLettuce() {
    return haveLettuce;
}

public void setHaveLettuce(boolean haveLettuce) {
    this.haveLettuce = haveLettuce;
}

public double getCostOfLettuce() {
    return costOfLettuce;
}

public void setCostOfLettuce(double costOfLettuce) {
    this.costOfLettuce = costOfLettuce;
}

}

vptzau2j

vptzau2j1#

更改此代码

if (addLettuce){
        Lettuce myLettuce1 = new Lettuce(true);
    }else{
        Lettuce myLettuce1 = new Lettuce(false);
        System.out.println("I am in Lettuce: " + myLettuce.getCostOfLettuce() + myLettuce.isHaveLettuce());
    }

至-

if (addLettuce){
            myLettuce = new Lettuce(true);
}else{
            myLettuce = new Lettuce(false);
            System.out.println("I am in Lettuce: " + myLettuce.getCostOfLettuce() + myLettuce.isHaveLettuce());
        }

相关问题