java - linked list NullPointerException -
i getting nullpointerexception
, don't know why.
linkedlist<character>[][] list = new linkedlist[n][n]; (int j = 0; j < n; ++j) { (int m = 0; m < 1; m++) { // here problem list[j][m].add("" + (characterarray[j])); } }
i want add characters array list. when run it says nullpointerexception
. don't know how initialize list.
the problem here created 2 dimensional array of linkedlist objects. remember default value objects in java null, when array first created have 2 dimensional array of nulls. in nested loops trying populate linkedlist objects think have don't (their value null). say
null.add("" + (characterarray[j]));
obviously creates null pointer exception.
so solution make object (in case make new linkedlist object) before trying add it. either having separate 2 nested loops follows:
for(int j = 0; j < n; j++) { for(int m = 0; m < n; m++) { list[j][m] = new linkedlist<charater>(); } }
followed code nested loops.
or can declare object inside nested loops
for (int j = 0; j < n; ++j) { (int m = 0; m < 1; m++) { // here problem list[j][m] = new linkedlist<charater>(); list[j][m].add("" + (characterarray[j])); } }
Comments
Post a Comment