Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Exception in thread "main" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question 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 :
    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
        }
     
    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Your problem is here:
    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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    MryJaho (February 4th, 2011)

  4. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Re: Exception in thread "main" java.lang.NullPointerException

    Quote Originally Posted by aussiemcgr View Post
    Your problem is here:
    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 0verticesi++ )
           {
               
    char element = (char)( 74 );
              
    tempList].setDataelement );
           }
    for ( 
    int i 0verticesi++ )
           {
               
              
    headNodes[i].setData(element)=tempList].setDataelement );
           }

               
       } 
    if i`m wrong please edit it..
    tnx agn..

  5. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Going back to your original code:
    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:
    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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    javapenguin (February 4th, 2011)

  7. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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..?

Similar Threads

  1. Replies: 6
    Last Post: November 12th, 2010, 03:40 AM
  2. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  3. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM
  4. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 01:25 AM
  5. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM