简体   繁体   English

为什么我收到错误 this.variable is null in java class

[英]Why I am getting error this.variable is null in java class

I am trying to write a small code in java.我正在尝试在 java 中编写一个小代码。 I am getting error from method getTotalCost().我从方法 getTotalCost() 中得到错误。 can you please check my code and point out what mistake i am doing.你能检查我的代码并指出我在做什么错误。

I am creating a class Hamburger.我正在创建一个 class 汉堡包。 passing 3 variables to consutuctor.将3个变量传递给constructor。 then its my choice to add tomato or spinach to my Burger.然后我选择在我的汉堡中加入番茄或菠菜。 In my getTotalCost() i am trying to print value if myLettuce.在我的 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)

Here is my Hamburger.java这是我的汉堡包。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());


}

Here is my Lettuce Class package com.amitsuneja;这是我的生菜 Class package com.amitsuneja;

public class Lettuce {公共 class 生菜 {

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;
}

} }

Change this code更改此代码

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

to -至 -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM