I am trying to create an undirected graph that is read from a text file. However I keep getting a NullPointerException. This is my Graph class:
Graph.java:
package testalgo;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Graph
{
ArrayList<Integer> vertices = new ArrayList<Integer>();
HashMap<Integer, LinkedList<Integer>> adj;
static Scanner sc;
public Graph(ArrayList<Integer> verts ){
adj =new HashMap<Integer, LinkedList<Integer>>();
}
public static void main(String[] args) {
try{
sc = new Scanner(new File("graph1.txt"));
}
catch(Exception e){
System.out.println(e);
}
while(sc.hasNext()){
int a = Integer.parseInt(sc.next());
int b = Integer.parseInt(sc.next());
Graph g = new Graph(new ArrayList<Integer>());
g.addVertex(a);
g.addeEgde(a, b); // this is line 46
g.addeEgde(b, a);
}
sc.close();
}
public void addVertex(int v){
for (int i = 1; i < vertices.size(); ++i) {
adj.put(i, new LinkedList<Integer>());}
}
public void addeEgde(int v1, int v2) {
adj.get(v1).add(v2); // this is line 68
}
public List<Integer> getNeighbors(int v) {
return adj.get(v);
}
}
And this is the error message that I am getting:
Exception in thread "main" java.lang.NullPointerException
at testalgo.Graph.addeEgde(Graph.java:68)
at testalgo.Graph.main(Graph.java:46)
Thank you for all your help!
I don't see anywhere in your code where you're populating the the adj map with v as a key. Hence adj.get(v1) will return null. You've only declared adj; you need to populate it as well.
All I see is:
for (int i = 1; i < vertices.size(); ++i) {
adj.put(i, new LinkedList<Integer>());
}
Since vertices is empty to begin with, you won't be inserting anything into your map.
Did you mean:
adj.put(v, new LinkedList<Integer>());
instead?
In response to your comment: you need to add another entry for b in your adjacency list:
g.addVertex(a);
g.addVertex(b); // <--- you need this as well
g.addeEgde(a, b);
g.addeEgde(b, a); // <--- otherwise you'll get a NPE here
You probably have invoked addeEdge without having added any vertex, as
for (int i = 1; i < vertices.size(); ++i)
{
adj.put(i, new LinkedList<Integer>());
}
Will not put any LinkedList instance in adj (vertices.size() is 0).
Therefore,
adj.get(v1).add(v2);
Will throw a NullPointerException, as adj.get(v1) will return null and you are invoking add on null.
Try declaring:
private static int count = 0;
In class body, and then, in addVertex:
adj.put(count++, new LinkedList<Integer>());
This one will put a new LinkedList in your Map every time addVertex is invoked.
Alternatively, for the map index:
public void addVertex(int vertexIndex)
{
adj.put(vertexIndex, new LinkedList<Integer>());
}
In addition, ensure that adj.get(v1) will not be null by making a null check comparison before calling add on it:
LinkedList<Integer> vertex = adj.get(v1);
if(vertex != null)
{
vertex.add(v2);
}
Also, cosider that g.addeEgde(b, a); applies to a vertex that does not exist and will be a source of NullPointerException too.
UPDATE: In the case you are attempting to insert values in a sequence (sequential index), using a Map for your vertexes is not the mostly efficient way of doing this (takes O(logn) complexity time of indexing). I suggest that you use an ArrayList instead:
ArrayList<LinkedList<Integer>> adj;
And in your addVertex:
adj.add(new LinkedList<Integer>());
And when indexing:
adj.get(yourIndex);
This one will be much more efficient (takes O(1) complexity time in indexing).
UPDATE 2: If your case is an adjacency list, which probably is, the Map is better to use than the ArrayList, as you do not have a sequential indexing.
Take in mind you must add vertices by adjacency value and NOT sequential indexing, as Vivin Paliath mentioned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With