I am creating an Graph using Adjacency Lists. However I am getting an error:
Exception in thread "main" java.lang.NullPointerException.
According to the error, it seems to be in the printGraph() function.
at graph.Graph.printGraph(Graph.java:120)
at graph.Graph.main(Graph.java:160)
Someone was able to help me and suggested the "try-catch". This removed the error, however it seems my printGraph() function still doesn't work. I think I might have an error with creating an object but I do not know how to fix this. Thank you!
Link to code:
https://stackoverflow.com/questions/48899811/graph...
Copyright © 2024 Q2A.MX - All rights reserved.
Answers & Comments
Verified answer
In your constructor is this:
// Create AdjList
LinkedList<EdgeNode>[] adjList = new LinkedList[nVertices];
This creates a local adjList which shadows the instance one and is discarded when the constructor ends.
I found the error because Eclipse colored "adjList" brown instead of blue.
Change the line to
adjList = new LinkedList[nVertices];
(Also: implementing a try-catch block to prevent a Null pointer error, then wondering why the code still doesn't work is like if your house is on fire and the smoke detectors are blaring and you poke holes in your ear drums to "fix the fire" then wonder why the room is still full of smoke)
the Null Pointer is caused by the printGraph creating a array list which is initially empty until the array gets filled with the data points, which printGraph doesnt check actually gets completed. so there is something wrong with the data/axis ranges. Try just before the call, setting the data manually as something is NOT correct in your list of data. so i am saying get any printGraph working before then allowing your list of data in, bit by bit.
you can try only universal exception.