4 Replies Latest reply on Oct 25, 2006 1:53 AM by 807598

    Linked List - nodes

    807598
      Can someone help me with my syntax? I'm creating a class ListADT with several methods in the class. In order to test my methods, I included a main method that will create a linked list with random numbers.
      I keep getting the following error message in my random method:

      "The local variable head may not have been initialized"

      I've declared and initialized (I think) my nodes as listed below.

      Can anyone tell me how to fix my error?
      public class Node {
      
                public int info;
                public Node link;
                
                public Node(){
                     //default constructor does nothing.
                     //Therefore info and link will get default values.
                     //For objects this will be null.
      Here's my random method:
      public Node buildListRandomly(int n){
           
                Node head,newNode;     // declare Nodes - head, newNode.
      
                Random num = new Random();
                
                for(int i = 1; i <= n; i++){
                     
                     newNode = new Node();
                     newNode.info = num.nextInt(100);
                     newNode.link = head;  //THIS  IS WHERE THE ERROR MESSAGE IS...
                     head = newNode;
                     
                }
                return head;  //THIS IS WHERE THE ERROR MESSAGE IS...
        • 1. Re: Linked List - nodes
          807598
          Node head,newNode;     // declare Nodes - head, newNode.

          This line did not initialize the head, and then it gets used at:

          newNode.link = head; //THIS IS WHERE THE ERROR MESSAGE IS...

          For example, you may initialize the head when it was declared:

          Node head = new Node ();
          • 2. Re: Linked List - nodes
            807598
            Can someone help me with my syntax? I'm creating a
            class ListADT with several methods in the class. In
            order to test my methods, I included a main method
            that will create a linked list with random numbers.
            I keep getting the following error message in my
            random method:

            "The local variable head may not have been
            initialized"

            I've declared and initialized (I think) my nodes as
            listed below.

            Can anyone tell me how to fix my error?
            public class Node {
            
                      public int info;
                      public Node link;
                      
                      public Node(){
                           //default constructor does nothing.
            //Therefore info and link will get default
            ult values.
                           //For objects this will be null.
            Here's my random method:
            public Node buildListRandomly(int n){
                 
            Node head,newNode;     // declare Nodes - head,
            d, newNode.
            
                      Random num = new Random();
                      
                      for(int i = 1; i <= n; i++){
                           
                           newNode = new Node();
                           newNode.info = num.nextInt(100);
            newNode.link = head;  //THIS  IS WHERE THE ERROR
            ROR MESSAGE IS...
                           head = newNode;
                           
                      }
            return head;  //THIS IS WHERE THE ERROR MESSAGE
            GE IS...
            As the error message points out,you have not initialized your variable.
            You just declared it.
            newNode.link = head;  //THIS  IS WHERE THE ERROR MESSAGE IS...
                           head = newNode;
            You are putting head into the newNode.link, but at that time the head does not contain anything.
            And in your loop, if the value of n is 0, it wont go inside the loop.
            It will try to return a uninitialized variable head, which does not contain
            anything. Thats why the error is appearing. You need to initialzie the variable.
            • 3. Re: Linked List - nodes
              807598
              Thanks, that got rid of my error message!!

              aiki
              • 4. Re: Linked List - nodes
                807598
                This may have removed error messages but I don't think it will solve your problem. The entire LinkedList has been created inside your method and will not be accessible outside that method.

                OK, ignore that. Just noticed you return head.

                Message was edited by:
                flounder