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 :
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 ); } } }
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 ; } }
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 } }