Jump to content
  • Advertisement
Sign in to follow this  
Devil0150

Java: NullPointerException

This topic is 2514 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

Hi
I keep getting this error in my code and I can't find how to fix it.
Exception in thread "main" java.lang.NullPointerException
at Field.<init>(Dam.java:73)

Here is part of the code:

class Position
{
boolean isEmpty;
Piece pHere;
int adjPos[]; // adjacent positions
void setPiece(Piece p)
{
isEmpty = false;
pHere = p;
}
Position()
{
adjPos = new int[12];
isEmpty = true;
}
}
class Field
{
Position pos[];
Field()
{
pos = new Position[13];
pos[0].adjPos[0] = 1; //error in this line
pos[0].adjPos[1] = 3;
pos[0].adjPos[2] = 5;
pos[0].adjPos[3] = 6;
}
}

Share this post


Link to post
Share on other sites
Advertisement
The problem is that when you create your position array,

[color=#000000]pos = new Position[13];

you make the assumption that the array is filled with Position objects. It is not.

You need:

[color=#000000]
pos = new Position[13];
pos[0] = new Position(); //set element 0 of Position Array to a Position Object.
pos[0].adjPos[0] = 1;



Make sense?

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!