Tuesday, July 31, 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

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...