As many have said in the previous answers, ResultList[] boll = new ResultList[5];
simply creates an array of ResultList having size 5 where all elements are null. When you are using boll[0].name
, you are trying to do something like null.name
and that is the cause of the NullPointerException. Use the following code:
public class Test {
public static void main(String[] args){
ResultList[] boll = new ResultList[5];
for (int i = 0; i < boll.length; i++) {
boll[i] = new ResultList();
}
boll[0].name = "iiii";
}
}
Here the for loop basically initializes every element in the array with a ResultList
object, and once the for loop is complete, you can use
boll[0].name = "iiii";