阵列没有初始化,我不明白为什么。
所以这是第一堂课
class Bag{
Potato[] potatoes = new Potato[10]; //so we have 10 bags of potatoes
}
其中包含了这个类的数组
class Potato{
int numb;
public void setNumb(int numb){
this.numb = numb;
}
String[] facts = new String[numb];//every potato has a numb of facts
}
我创建了对象Bag,所以我假设它包含一个类Potato的数组,它包含一个String事件数组,但它们甚至没有被初始化:" String [] facts = new String [麻木]"
public class test{
public static void main (String[ ] args){
Bag newBag = new Bag();
newBag.potatoes[0].setNumb(2);
newBag.potatoes[0].facts[0]="firstFact";
System.out.println(newBag.potatoes[0].facts[0]);
}
}
错误:
Exception in thread "main" java.lang.NullPointerException