Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Java: NullPointerException


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Devil0150   Members   -  Reputation: 153

Like
0Likes
Like

Posted 30 January 2012 - 09:51 AM

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;
}
}



Sponsor:

#2 Zael   Members   -  Reputation: 154

Like
0Likes
Like

Posted 30 January 2012 - 10:31 AM

The problem is that when you create your position array,

pos = new Position[13];


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

You need:

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?

#3 Devil0150   Members   -  Reputation: 153

Like
0Likes
Like

Posted 30 January 2012 - 01:19 PM

It worked. Thanks.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS