mainMethod.java
public class mainMethod{
public animalsData[] animals;
public mainMethod(){
animals[this.animals.length + 1] = new animalsData("CAT", "4");
}
public static void main(String[] args) {
mainMethod run = new mainMethod();
}
}
animalsData.java
public class animalsData{
String name, l;
public animalsData(String name, String l) {
super();
this.name= name;
this.l= l;
}
}
I hava this problem: Exception in thread “main” java.lang.NullPointerException
You are never initializing your
animalsarray in yourmainMethodclass.In your
public mainMethod()method, you need to doanimals = new animalsData[INITIAL SIZE];If you want it to grow automatically, you should use a
List. Even then you would not usethis.animals.length + 1as its index. you would simply doList.add()