So, I have a class Square and I am trying to use an array for it board. Here is my code:
public class Square{
public int pcolor;
public int contains;
public int xPos;
public int yPos;
Square(int xp,int yp,int pc,int cont){
xPos=xp;
yPos=yp;
contains=cont;
pcolor=pc;
}
};
Square[] board = new Square[64];
board[0].xPos=0;
This gives me unexpected token: [ on board[0].xpos=0;. Can anyone help me resolve this?
EDIT:
OK, I moved board[0].xpos=0; inside a method; now it gives me NullPointerException. What do I do?
You are trying to make a statement not inside a method or a static scope.
The statement
board[0].xPos = 0;should [probably] be inside a method.You also seem to have a redundant
};This code compiles just fine:
To initialize [and access] elements in
board– you will have to do it in a method or in the constructor.