Value is null even after i have allocated memory for the object
Hi,
I am a total newbie in java , and i am facing this issue. after allocating memory for my class instance, i am not able to access the variable in this instance.
here is the code for this.
Main function:
CreateGraph:
Code java:
Graph createGraph(int v,Graph g1){
g1.numv=v;
g1.g=new Vertex[v];
// g1.g[0].id=4;
System.out.println("value of g[0].id is"+g1.g[0].id );
for (int i=0;i<v;i++)
{
g1.g[i]=new Vertex();
g1.g[i].id=i;
}
return g1;
The output i am getting is :
Exception in thread "main" java.lang.NullPointerException
at graphcolouring.Graph.createGraph(Graph.java:21)
at graphcolouring.GraphColouring.main(GraphColouring. java:29)
Re: Value is null even after i have allocated memory for the object
What variable is null in that line?
Also, declaring a variable does not initialize it. Until you initialize it, the variable's value is null.