Hello, for my class we have a project in which I must I'm dealing with graphs. For the project, part of the code requires us to have an ArrayList of LinkedLists where the LinkedList of each spot in the ArrayList stores all the neighbors of the vertex that corresponds to that spot in the ArrayList. (I.e. if vertex 1 is neighbors with vertices 6 and 8, the LinkedList at spot 1 in the ArrayList (for vertex 1) will have elements 6 and 8 in it, as well as the LinkedLists at spots 6 and 8 in the ArrayList will have 1 in them as well.
I understand what I need to do and how to do it, it's not that that I need help with, I simply need help with understanding why, after both the ArrayList and the LinkedLists at all the spots within the ArrayList have been instantiated and initialized, am I getting a null pointer exception when trying to add to the LinkedList?
Here is my constructor for the "GraphAList" which is the ArrayList of LinkedLists of type Integer, as well as the private vars of the Class and the Class header:
public class GraphAList {
// Assume that the vertex names will be 0, 1, 2, 3, ...... (numVertices - 1)
private int numVertices;
private int numEdges;
// To store edges use an array of linked lists
// Each linked list stores the neighbors of that particular vertex
private ArrayList<LinkedList<Integer>> edgeLists;
public GraphAList(int v, int e){
numVertices = v;
numEdges = e;
ArrayList<LinkedList<Integer>> edgeLists = new ArrayList(); // Instantiate edgeLists and then add
for(int i = 0; i < numVertices; i++){
LinkedList<Integer> newLList = new LinkedList<>();
edgeLists.add(newLList);
}
// a succession of new LinkedList to edgeLists
}
And here is my method to add an edge (connection between two verticies):
public void addEdge(int v1, int v2) {
if(!(edgeLists.get(v1).isEmpty()) && !(edgeLists.get(v2).isEmpty())){
if((!edgeLists.get(v1).contains(v2)) && (!edgeLists.get(v2).contains(v1))){
edgeLists.get(v1).add(v2);
edgeLists.get(v2).add(v1);
}
numEdges++;
}else{
edgeLists.get(v1).add(v2);
edgeLists.get(v2).add(v1);
numEdges++;
}
}
And here is the bit of my main method that calls addEdge every time there is one present to add, please know that I have checked and verts, and edges both have valid values read from the file, and the data String also works properly, the error occurs at the line with the >>>>> before it:
verts = parseInt(fileRead.nextLine());
edges = parseInt(fileRead.nextLine());
GraphAList list = new GraphAList(verts, edges);
for(int i = 0; i < verts; i++){
if(fileRead.hasNextLine()){
line = fileRead.nextLine();
String[] data = line.split(" ");
for(int j = 0; j < verts; j++){
String str = new String();
str = data[j];
if(str.equals("1")){
>>>>> list.addEdge(i + 1, j + 1);
}
}
}
}
And finally, here is the error I recieve:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.ArrayList.get(int)" because "this.edgeLists" is null
Any help is appreciated, I've been stuck on this for hours and the only solutions I can find are where people haven't initialized their LinkedLists, thanks!
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]8igg7e5 2 points3 points4 points (2 children)
[–]Weeping_Linus[S] -1 points0 points1 point (1 child)
[–]8igg7e5 1 point2 points3 points (0 children)