Exception in thread "main" java.lang.NullPointerException
Exception in thread "main" java.lang.NullPointerException
at testapp.Graph.setHeadNodesData(Graph.java:44)
at testapp.Main.main(Main.java:19)
Dear myFriends..Hi..!
i dono know what is this error for..
( I implemented a graph using adjacency list..)
here`s my classes :
Code Java:
public class Graph{
private int vertices;
private int defaultSize = 1;
private List [] headNodes ;
private int maxRandom = 10;
static final double defaultWeight = 0;
//public Vars:
public Graph( int n ) {
vertices = n;
headNodes = new List[ vertices ];
}
public void setHeadNodesData()
{
for ( int i = 0; i < vertices; i++ )
{
char element = (char)( i + 74 );
headNodes[ i ].setData( element );
}
}
public void WeighteningGraph() {
Random generator = new Random();
for ( int i = 0; i < vertices; i++ )
{
int n = 1 + generator.nextInt( maxRandom); // 0 <= n < a
headNodes[i].setWeight(n);
}
}
public void print() {
for ( int i = 0; i < vertices; i++ )
{
char element = headNodes[ i ].getData();
System.out.print( element );
}
}
}
Code Java:
public class List {
private char data;
private ListNode link = new ListNode();
private int weight;
public List( char element) {
data = element;
link = null;
}
public char getData() { return data; }
public void setData( char element) { data = element; }
public void setWeight( int randWeight ) {
weight = randWeight ;
}
}
Code Java:
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Graph g1 = new Graph( 10 );
g1.setHeadNodesData();
g1.print();
// TODO code application logic here
}
}
Re: Exception in thread "main" java.lang.NullPointerException
Your problem is here:
Code java:
public void setHeadNodesData()
{
for ( int i = 0; i < vertices; i++ )
{
char element = (char)( i + 74 );
headNodes[ i ].setData( element );
}
}
When you create an array of Lists, each spot in that array is set to null. Java will not automatically fill your array with List objects. So, when you use the headNodes[i] call, it is returning null. What you need to do is create a List object, use the setData(element) method on that new List object, then set the headNodes[i] to be that new List object.
Re: Exception in thread "main" java.lang.NullPointerException
Quote:
Originally Posted by
aussiemcgr
Your problem is here:
Code java:
public void setHeadNodesData()
{
for ( int i = 0; i < vertices; i++ )
{
char element = (char)( i + 74 );
headNodes[ i ].setData( element );
}
}
When you create an array of Lists, each spot in that array is set to null. Java will not automatically fill your array with List objects. So, when you use the headNodes[i] call, it is returning null. What you need to do is create a List object, use the setData(element) method on that new List object, then set the headNodes[i] to be that new List object.
tnx for answering..
but..i dont know what`s the diffrent?
if u mean so:
PHP Code:
public void setHeadNodesData()
{
List tempList = new List[vertices];
for ( int i = 0; i < vertices; i++ )
{
char element = (char)( i + 74 );
tempList[ i ].setData( element );
}
for ( int i = 0; i < vertices; i++ )
{
headNodes[i].setData(element)=tempList[ i ].setData( element );
}
}
if i`m wrong please edit it..
tnx agn..
Re: Exception in thread "main" java.lang.NullPointerException
Going back to your original code:
Code java:
public Graph( int n ) {
vertices = n;
headNodes = new List[ vertices ];
}
public void setHeadNodesData()
{
for ( int i = 0; i < vertices; i++ )
{
char element = (char)( i + 74 );
headNodes[ i ].setData( element );
}
}
There are technically two types of array you can create by using the x[] varName = new x[size]; syntax. One type is an array of primitives (ints,doubles,booleans,ect.) and the second is an array of Objects. When JAVA creates an array of primitives or Objects, it sets each index in the array to the identifier's default value. For Objects, the default value is null, but for primitives the default value depends on the primitives (Java Quick Reference - Language Fundamentals - Default Values).
So, when you create an array of Lists by saying List[] headNodes = new List[vertices];, each index in the array is set to null because List is an Object. This means that you cannot use any of the List methods on an index in the array until you put a List into that index.
So, when you say headNodes[ i ].setData( element );, you are accessing index i and attempting to use the setData() method on it. The problem is that headNodes[i]'s value is null, not a List object. So the first thing that we need to do is add List objects to the array. I would recommend doing this when you create your array, but with your current design you can actually do that in the setHeadNodesData() method.
So what you want to do is this:
Code java:
public void setHeadNodesData()
{
for ( int i = 0; i < vertices; i++ )
{
char element = (char)( i + 74 );
//Create a new List Object and send it element
List tempList = new List(element);
//Set the index in headNodes to be tempList so it now contains a List object
headNodes[ i ] = tempList;
}
}
Tell me if this makes sense. I want to make sure you understand the information above instead of just taking the solution.
Re: Exception in thread "main" java.lang.NullPointerException
at first i have a main problem..i dont which IDE u use..i use Netbeans 6.5 ..
at first that i start a new project i have a main class..then define some other classes in that source package..
now when i want to use objects of classes in main there is some error( cant find symbol class className )..
how can i solve this problem..?