【发布时间】:2014-04-15 19:19:39
【问题描述】:
我在main方法中写了这个语句
异常将在 (GuaranteeYears) 中,所以我将它放在 try 块中。
首先在main 中创建一个Store 对象并在参数中传递数字4,在Store 类中我将有一个名为arrProduct 的数组,其长度为4:
boolean flag1 = true;
while (flag1) {
try {
System.out.println("Ente GuaranteeYears");
int GuaranteeYears=read.nextInt();
StrongDevice s = new StrongDevice(code,type,price,Model,Capacity,GuaranteeYears); //the object
flag1 = false;
x.addProduct(s);
}
这是我的添加方法
public boolean addProduct(Product p) {
if(arrProduct.length==NumOfProduct)
return false;
arrProduct[NumOfProduct]=p;
NumOfProduct++;
return true;
}
我的问题是:当我创建对象并将其添加到数组时,它给了我NullPointerException - 为什么会这样?
【问题讨论】:
-
@SotiriosDelimanolis 我真的知道什么是空指针异常,但我不知道为什么会在这里发生?
-
NPE 发生的原因没有一百万个。阅读答案。您会发现它发生的原因,并将其应用到您的场景中。