Jump to content
  • Advertisement
Sign in to follow this  
deadlydog

Beginner Java Problem

This topic is 5053 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
private CQueueItem[] cQueue = new CQueueItem[101];

This line only initulizes an array it dose not intilize the elements of the array. You need to initulize them yourself.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!