Java - How to declare table[i][j] elements as instance variables?
All,
I am trying to code a Connect4 game. For this, I have created a P4Game class and a P4Board class which represents the i X j dimensions of the Connect4 board.
In P4Game, I have the following:
public class P4Game{
//INSTANCE VARIABLES
private int nbLines;
private int nbColumns;
private P4Board [][] position;
//CONSTRUCTOR
public P4Game(int nbLines, int nbColumns){
this.nbColumns = nbColumns;
this.nbLines = nbLines;
P4Board [][] position = new P4Board [nbLines][nbColumns]; //Creates the table to receive the instances of the P4Board object.*/
for (int i=0; i<nbLines; i++){
for (int j=0; j<nbColumns; j++){
this.position[i][j] = new P4Board(i,j); //Meant to create each object at (line=i, column=j)
}开发者_JAVA百科
}
}
This causes a NullPointerException in the nested loops where I mention this.position[i][j]
. I reference those objects in other methods of this class so I need them to be instance variables. I suppose the exception is due to the fact that I have not listed the table element position[i][j]
as an instance variable at the beginning of the class.
my question to people here is (1) is my assumption correct, and if so (2) what would be the syntax to declare instance variables of this form?
Thank you all for your help with what I realize is a very basic question. Hopefully it will also benefit other newbies.
Cheers,
JDelage
See added comment inlined... You code is fine except for one little detail, where you're creating a new position
variable where you actually mean to use the instance variable.
public class P4Game{
//INSTANCE VARIABLES
private int nbLines;
private int nbColumns;
private P4Board [][] position;
//CONSTRUCTOR
public P4Jeu(int nbLines, int nbColumns){
this.nbColumns = nbColumns;
this.nbLines = nbLines;
// You're creating a LOCAL variable called position here if you don't comment what's commented:.
/*P4Board [][] */position = new P4Board [nbLines][nbColumns]; //Creates the table to receive the instances of the P4Board object.*/
for (int i=0; i<nbLines; i++){
for (int j=0; j<nbColumns; j++){
this.position[i][j] = new P4Board(i,j); //Meant to create each object at (line=i, column=j)
}
}
}
}
Your assumption is incorrect.
In the constructor, you're making a local variable with the same name as the field. (By writing P4Board [][]
position = ...
) This creates a local variable and does not affect the field, which remains uninitialized. You need to remove the P4Board [][]
to change it from a variable declaration to an assignment of the existing field. (Just like you write this.nbLines = ...
to assign the field)
You're redefining P4Board [][] position
in the constructor and then calling this.position
which is not initialized (i.e. null).
Look out carefully! You are hiding the instance variable > P4Board [][] position = new P4Board [nbLines][nbColumns];
As others have said you are hiding the instance variable with a local variable. You should really check out checsktyle as it has checks to tell you if you have made such a mistake. Two other tools are PMD and FindBugs.
Your assumption is incorrect. Try looking a few lines higher for the bug in your homework.
This runs for me. I substituted into for P4Board, since you didn't supply it:
public class P4Game
{
private int nbLines;
private int nbColumns;
private int [][] position;
public static void main(String[] args)
{
P4Game game = new P4Game(3, 3);
System.out.println(game);
}
public P4Game(int nbLines, int nbColumns)
{
this.nbColumns = nbColumns;
this.nbLines = nbLines;
this.position = new int[this.nbLines][this.nbColumns];
for (int i=0; i < this.nbLines; i++)
{
for (int j=0; j < this.nbColumns; j++)
{
this.position[i][j] = i+j;
}
}
}
public String toString()
{
StringBuilder builder = new StringBuilder(1024);
builder.append('[');
for (int i = 0; i < this.nbLines; ++i)
{
builder.append('{');
for (int j = 0; j < this.nbColumns; ++j)
{
builder.append(this.position[i][j]).append(',');
}
builder.append('}');
}
builder.append(']');
return builder.toString();
}
}
精彩评论