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 4 of 4

Thread: Priority Queue Null Pointer Exception

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Priority Queue Null Pointer Exception

    Hello!

    I am currently working on an assignment and I have been stuck with a NullPointerException.

    I am making a Priority Queue (PQ) as one part of the assignment i'm working on.

    Firstly, if I add nodes into the queue in ascending order I do not get an error while adding to the queue (1,3,5,6, etc), but I do get the error when putting them out of order (4,2,5).

    So this tells me there is a problem in my balance function.

    Secondly, when removing Nodes from the PQ (input ascending order 4,5,6) it keeps removing the first one only (4,4,4). So there is some problem with my "leave" function.

    This is the exact error:
    "java.lang.NullPointerException
    at HEAP.PQ.balance(PQ.java:68)
    at HEAP.PQ.enter(PQ.java:54)
    at HEAP.Dijkstras.<init>(Dijkstras.java:28)
    at HEAP.Dijkstras.main(Dijkstras.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)"



    Any help with finding where the error is occurring would be greatly appreciated!

    Also any other suggestions are welcomed.

    Thanks in advance!

    Here is my PQ class:

    public class PQ
    {
      public Node[] array = new Node[30];
      public int max = 0;
     
      public PQ()
      {
     
      }
     
      //function to check if the PQ is empty, returns true if it is
      public boolean isEmpty()
      {
        if(array[1] == null)
          return true;
        else
          return false;
      }
     
      //function to add to the PQ
      public void enter(Node n)
      {
        int count = 1;
     
        if(array[1] == null)
        {
          array[1] = n;
        }
     
        else
        {
        while(array[count] != null)
        {
          count++;
          System.out.println(count);
        }
     
        array[count] = n;
        }
     
        max = max + 1;
        balance(max);
      }
     
      //function to balance the PQ after an add
      private void balance(int count)
      {
        Node temp;
        if(array[2] == null && array[3] == null)
        {
          return;
        }
     
        else
        {
          while(array[count/2].key > array[count].key)
          {
            if(count == 1)
            {
              if(array[1].key > array[count].key)
              {
                temp = array[1];
                array[1] = array[count];
                array[count] = temp;
              }
     
              break;
            }
     
            temp = array[count/2];
            array[count/2] = array[count];
     
            array[count] = temp;
            count = count/2;
     
          }
        }
      }
     
      //function to remove from the PQ
      public Node leave()
      {
        Node temp;
     
        //check if array only has 1 node
        if(array[2] == null && array[3] == null)
        {
          temp = array [1];
          array[1] = null;
          max = max - 1;
          return temp;
        }
     
        temp = array [1];
          array[1] = array[max];
          array[max] = null;
          topBal(1);
          max = max - 1;
          return temp;
      }
     
      //function to balance PQ after remove
      private void topBal(int temp)
      {
        Node temp2;
     
        if(array[1] == null)
          return;
     
        else
        {
          //only if right is null, if so check if left is greater, if so swap
          if(array[temp*2] != null && array[(temp*2)+1] == null)
          { 
     
            if(array[temp*2].key > array[temp].key)
            {
             temp2 = array[temp]; 
             array[temp] = array[temp*2];
             array[temp*2] = temp2;
             topBal(temp*2);
            }
          }
     
           //only if left is null, if so check if right is greater, if so swap
          if(array[temp*2] == null && array[(temp*2)+1] != null)
          { 
            if(array[(temp*2)+1].key > array[temp].key)
            {
             temp2 = array[temp]; 
             array[temp] = array[(temp*2) + 2];
             array[(temp*2)+1] = temp2;
             topBal((temp*2)+1);
            }
          }
     
          //if both are not null, check which is larger & if they are larger then current node, swap if so
          if(array[temp] != null && array[temp*2] != null && array[(temp*2) + 1] !=null)
          {
            //if left is larger
           if(array[temp*2].key > array[(temp*2)+1].key && array[temp*2].key > array[temp].key)
           {
             temp2 = array[temp];
             array[temp] = array[temp*2];
             array[temp*2] = temp2;
             topBal(temp*2);
           }
     
           //if right is larger
           if(array[temp*2].key < array[(temp*2)+1].key && array[(temp*2)+1].key > array[temp].key)
           {
             temp2 = array[temp]; 
             array[temp] = array[(temp*2) + 2];
             array[(temp*2)+1] = temp2;
             topBal((temp*2)+1); 
           }
          }
        }
      }
    }

  2. The Following User Says Thank You to OverZealous For This Useful Post:

    ranjeet1523 (March 8th, 2014)


  3. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Priority Queue Null Pointer Exception

    at HEAP.PQ.balance(PQ.java:68)

    Something on line 68 is null. Add print statements before this line to find out what is null. Once you know what is null then trace your code and find out why it is null.
    Improving the world one idiot at a time!

  4. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Priority Queue Null Pointer Exception

    Thank you sir, I didn't know that was the line the error was on!

    I will check it out and let you know how it goes.

  5. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Priority Queue Null Pointer Exception

    Thanks, I found what was causing the problem for this!

Similar Threads

  1. [SOLVED] Null Pointer Exception
    By wltrallen2 in forum Object Oriented Programming
    Replies: 7
    Last Post: May 27th, 2012, 10:21 AM
  2. Help with null pointer exception
    By Dr.HughMan in forum Exceptions
    Replies: 35
    Last Post: November 30th, 2011, 09:00 PM
  3. Null Pointer Exception?
    By SeanEE89 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 16th, 2011, 06:21 PM
  4. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 10:07 AM
  5. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM