为什么我得到错误this.variable在java类中为null



我正在尝试用java编写一个小代码。我从方法getTotalCost((中得到错误。你能检查一下我的代码并指出我犯了什么错误吗。

我正在创建一个类汉堡。将3个变量传递给consutctor。然后我选择在汉堡里加番茄或菠菜。在我的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)

这是我的Hamburger.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;
}

}

更改此代码

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

最新更新