当前位置: 首页 > 知识库问答 >
问题:

无法调用“inventory.getinventoryamount()”,因为“this.inventory”为空

闻人修明
2023-03-14

这里有两节课。SaleGood类有一个可变特性a,应该在另一个名为Inventory类中使用。类Inventory有两个可变特性,inventoryAmount和inventoryBill,这两个特性都应该在类Salegood中使用。

我正在使用getter/setter。然而,错误说

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Inventory.getInventoryAmount()" because "this.inventory" is null
    at SaleGood.customerSituation(SaleGood.java:190)

我以为是因为这个代码错了?因此我不能使用inventory.getinventoryamount()。我试了一整天。但仍然不知道如何修复它。我尝试了setinventory(inventory).getinventoryamount,也是错误的。

public void setInventory(Inventory inventory ) {this.inventory = inventory ;}
import java.util.Scanner;

    public class SaleGood{
    private String featureA;
    Inventory inventory;
    public void setInventory(Inventory inventory ) {this.inventory = inventory ;}
    public Scanner getScannerPublic(){return scanner;}

        public static void main(String[] args) {
        SaleGood saleGood= new SaleGood();
        saleGood.customerSituation();}

    private void customerSituation() {
        do {
            Scanner scanner = getScannerPublic(); 
            System.out.print("Type answer here: "); 
            boolean flagP = true;
            do {
                if (scanner.hasNextLine()) { 
                    String keyBoard= scanner.nextLine(); 
                    switch (keyBoard) {
                        case "firstCase":
                            System.out.println("Customer might like blue item! ");
                            break;
                        case "secondCase":
                            System.out.println("What is featureA?");
                            featureA = scanner.nextLine();
                            System.out.println(getfeatureA() +" and "+ 
                                        inventory.getInventoryBill() + " and "
                                        inventory.getInventoryAmount()) 

                            flagP= false;
                            break;
                    } } } while (flagP); 
        }while (true); }

    public String getFeatureA(){ return featureA; }


} // The whole class end

在课堂上我有

public class Inventory{

    private int inventoryAmount; 
    private int inventoryBill; 
    private String featureA;

    private SaleGood saleGood;

    public void setSaleGood(SaleGood saleGood) {
        this.saleGood= saleGood;
    }

    
    public String getFeatureA(){
        return featureA= SaleGood.getFeatureA();
    }
    

    public int getInventoryBill(){
        return inventoryBill= 1000 ;
    }
   

    public int getInventoryAmount(){
        return inventoryAmount = 348965;
    }
}

共有1个答案

周云
2023-03-14

异常明确地告诉您这个问题:

线程“main”java.lang.NullPointerException中出现异常:无法调用“inventory.getInventoryAmount()”,因为“this.inventory”在Salegood.CustomerSonity(salegood.java:190)中为空

Salegood类没有构造函数,成员声明不包括任何初始化。因此,默认情况下,该类实例中的所有成员引用都为null。您为它提供了一个setter,但不调用它。

public class SaleGood {
  private Inventory inventory = new Inventory();
  ...
}
public class SaleGood {
  private Inventory inventory;
  public SaleGood() {
    inventory = new Inventory();
  }
  ...
}
 类似资料: