The arrays don't get initialized and I'm not understanding why.
So this is the first class
class Bag{
Potato[] potatoes = new Potato[10]; //so we have 10 bags of potatoes
}
which cointains an array of this class
class Potato{
int numb;
public void setNumb(int numb){
this.numb = numb;
}
String[] facts = new String[numb];//every potato has a numb of facts
}
I created the object Bag so i assumed it contains an array of the class Potato, which contains an array of String facts, but they aren't initialized even tho: "String[] facts = new String[numb]"
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]);
}
}
the error:
Exception in thread "main" java.lang.NullPointerException