Tuesday 31 July 2018

arrays - java.lang.NullPointerException in LinkedList[] add() method




I'm having problem when i'm adding node in Array of LinkedList.



int vertices = 100;
LinkedList[] AdjList = new LinkedList[vertices+1];


for (i = 1; i <= vertices; ++i) {

for (j = 1; j <= 6 && j + i <= vertices; ++j) {

AdjList[i].add(j); //Error occurs here.
}
}

Answer



You need to create the list before inserting into it. In your first for loop add this line, before the second for loop



AdjList[i] = new LinkedList();


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...