ArrayList<String> NullPointerException

Posted by Carlucho on Stack Overflow See other posts from Stack Overflow or by Carlucho
Published on 2010-04-20T03:11:09Z Indexed on 2010/04/20 3:13 UTC
Read the original article Hit count: 264

Am trying to solve a labyrinth by DFS, using adj List to represent the vertices and edges of the graph. In total there are 12 nodes (3 rows[A,B,C] * 4 cols[0,..,3]). My program starts by saving all the vertex labels (A0,..C3), so far so good, then checks the adjacent nodes, also no problems, if movement is possible, it proceeds to create the edge, here its where al goes wrong.

   adjList[i].add(vList[j].label);

I used the debugger and found that vList[j].label is not null it contains a correct string (ie. "B1"). The only variables which show null are in adjList[i], which leads me to believe i have implemented it wrongly. this is how i did it.

public class GraphList {
   private ArrayList<String>[] adjList;
   ...
   public GraphList(int vertexcount) {
      adjList = (ArrayList<String>[]) new ArrayList[vertexCount];
      ...
   }
   ...
   public void addEdge(int i, int j) {  
      adjList[i].add(vList[j].label);    //NULLPOINTEREXCEPTION HERE
   }
   ...
}

I will really appreaciate if anyone can point me on the right track regrading to what its going wrong... Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about nullpointerexception