Sign in to follow this  
deadlydog

Beginner Java Problem

Recommended Posts

Hey there. This is my first java program. I'm just trying to make a priority queue, but I can't get it to work. It compiles fine, but when I go to run it I get the following errors: java.lang.NullPointerException at Assignment1$CPriorityQueue.<init>(Assignment1.java:46) at Assignment1.main(Assignment1.java:8) Exception in thread "main" Here's my code:
// Main Class
public class Assignment1
{    
  public static void main(String[] args)
  {
    int iIndex = 0;
    
    CPriorityQueue cQueue = new CPriorityQueue();  // Line 8
    
    cQueue.Push(3, 3);
    cQueue.Push(4, 4);
    cQueue.Push(0, 0);
    cQueue.Push(5, 5);
    cQueue.Push(0, 0);
    cQueue.Push(7, 7);
    cQueue.Push(1, 1);
    cQueue.Push(6, 6);
    cQueue.Push(2, 2);
    cQueue.Push(3, 3);
    
    for (iIndex = 0; iIndex < 10; iIndex++)
    {
        System.out.println(cQueue.Pop() + " is popped");
    }
   
  }
 
  // Priority Queue Class
  public static class CPriorityQueue
  {
      class CQueueItem
      {
        int iItem;
        int iPriority;
      }
      
      private CQueueItem[] cQueue = new CQueueItem[101];
     
     // Class Constructor
     CPriorityQueue()
     {
         int iIndex = 0;
         
         for (iIndex = 0; iIndex < 101; iIndex++)
         {
             cQueue[iIndex].iItem = -1;       // Line 46
             cQueue[iIndex].iPriority = -1;
         }
     }
     
     // Push and Item into the Queue with it's Priority. 0 = Highest Priority, 1 = High Priority, etc.
     void Push(int iItem, int iPriority)
     {
         int iIndex = 0;
         int iItemSpot = 0;
         
         // Make sure Priority is within range
         if (iPriority < 0)
         {
             iPriority = 0;
         }
         
         // Find where Item should go in the Priority Queue
         while (iPriority > cQueue[iIndex].iPriority || cQueue[iIndex].iPriority != -1)
         {
             iIndex++;
         }
         
         // Save where Item should go
         iItemSpot = iIndex;
         
         // Move all items behind where this one should go back one spot
         for (iIndex = 101; iIndex > iItemSpot + 1; iIndex--)
         {
             cQueue[iIndex].iItem = cQueue[iIndex - 1].iItem;
             cQueue[iIndex].iPriority = cQueue[iIndex - 1].iPriority;
         }
         
         cQueue[iItemSpot].iItem = iItem;
         cQueue[iItemSpot].iPriority = iPriority;
     }
     
     /* Returns the item in the Queue with the highest priority */
     int Pop()
     {
         int iIndex = 0;
         int iItem = cQueue[0].iItem;
         
         // Move all item up one spot
         for (iIndex = 0; iIndex < 100; iIndex++)
         {
             cQueue[iIndex].iItem = cQueue[iIndex + 1].iItem;
             cQueue[iIndex].iPriority = cQueue[iIndex + 1].iPriority;
         }
         
         // Return the Item with the Highest Priority
         return iItem;
     }
     
  }
}

Can anybody tell me why it's not working. Thanks.

Share this post


Link to post
Share on other sites
Change your constructor like this:


// Class Constructor
CPriorityQueue()
{
int iIndex = 0;

for (iIndex = 0; iIndex < 100; iIndex++)
{
cQueue[iIndex] = new CQueueItem();
cQueue[iIndex].iItem = -1; // Line 46
cQueue[iIndex].iPriority = -1;
}
}


Basically, you have to create each one of the CQueueItems before you can use them. That's very confusing for someone coming from C++. Creating a new array of objects in Java really only creates an array of references to objects.

Hope this helps,

Tony

Share this post


Link to post
Share on other sites
Thanks, I did that and now I am getting the following error:

java.lang.ArrayIndexOutOfBoundsException: 101
at Assignment1$CPriorityQueue.Push(Assignment1.java:65)
at Assignment1.main(Assignment1.java:10)
Exception in thread "main"


// Main Class
public class Assignment1
{
public static void main(String[] args)
{
int iIndex = 0;

CPriorityQueue cQueue = new CPriorityQueue();

cQueue.Push(3, 3); // Line 10
cQueue.Push(4, 4);
cQueue.Push(0, 0);
cQueue.Push(5, 5);
cQueue.Push(0, 0);
cQueue.Push(7, 7);
cQueue.Push(1, 1);
cQueue.Push(6, 6);
cQueue.Push(2, 2);
cQueue.Push(3, 3);

for (iIndex = 0; iIndex < 10; iIndex++)
{
System.out.println(cQueue.Pop() + " is popped");
}

}

// Priority Queue Class
public static class CPriorityQueue
{
class CQueueItem
{
int iItem;
int iPriority;
}

private CQueueItem[] cQueue = new CQueueItem[101];

// Class Constructor
CPriorityQueue()
{
int iIndex = 0;

for (iIndex = 0; iIndex < 101; iIndex++)
{
cQueue[iIndex] = new CQueueItem();
cQueue[iIndex].iItem = -1;
cQueue[iIndex].iPriority = -1;
}
}

// Push and Item into the Queue with it's Priority. 0 = Highest Priority, 1 = High Priority, etc.
void Push(int iItem, int iPriority)
{
int iIndex = 0;
int iItemSpot = 0;

// Make sure Priority is within range
if (iPriority < 0)
{
iPriority = 0;
}

// Find where Item should go in the Priority Queue
while (iPriority > cQueue[iIndex].iPriority || cQueue[iIndex].iPriority != -1) // Line 65
{
iIndex++;
}

// Save where Item should go
iItemSpot = iIndex;

// Move all items behind where this one should go back one spot
for (iIndex = 101; iIndex > iItemSpot + 1; iIndex--)
{
cQueue[iIndex].iItem = cQueue[iIndex - 1].iItem;
cQueue[iIndex].iPriority = cQueue[iIndex - 1].iPriority;
}

cQueue[iItemSpot].iItem = iItem;
cQueue[iItemSpot].iPriority = iPriority;
}

// Returns the item in the Queue with the highest priority
int Pop()
{
int iIndex = 0;
int iItem = cQueue[0].iItem;

// Move all item up one spot
for (iIndex = 0; iIndex < 100; iIndex++)
{
cQueue[iIndex].iItem = cQueue[iIndex + 1].iItem;
cQueue[iIndex].iPriority = cQueue[iIndex + 1].iPriority;
}

// Return the Item with the Highest Priority
return iItem;
}

}
}



Can you tell me what's wrong with it now? Thanks again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this