0

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?

5
  • Please post some more code. This looks like a syntax error. Commented Feb 24, 2012 at 14:00
  • The error is on a different line, please post more of your code. Commented Feb 24, 2012 at 14:00
  • sorry, when i edited it i deleted it :X Commented Feb 24, 2012 at 14:26
  • This is not the full code then. Anyone who will compile this will get a compile-time error that the statements on the bottom are unexpected (they need to be in a method in the same or different class. And then get a run-time null pointer exception that you are not setting each array value to a new Square object. Commented Feb 24, 2012 at 14:30
  • For your edit initialize every value to a new object. board[0] = new Square(num, num, num, num); Commented Feb 24, 2012 at 15:23

3 Answers 3

3

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:

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

To initialize [and access] elements in board - you will have to do it in a method or in the constructor.

Sign up to request clarification or add additional context in comments.

8 Comments

Why is Square[] board inside of class Square
@user1188752: I have no idea what you are actually trying to do. It is just a sample of a code that is compiling. Without more information on what you are trying to achieve - I could only guess.
Chess game in Processing - Square is one square on the board
@user1188752 Then Square[] board should probably be inside a Game class or something indeed. Now that you have solved the compilation error - note that Square[] board = new Square[64]; does not create Squares. It only create the array, holding nulls. You will need to initialize board[i] = new Square(...); for each i.
@user1188752: Try to "play with it", and see if you can figure it out. If not - avoid editting this question with new issues - you should post a new question - describing the new situation and the new problem. I am sure someone will answer you pretty quickly...
|
1

Well, if you do this correctly you will get NullPointerException because you didn't create any object yet. My guess is you did some syntax error.

4 Comments

What do you mean, "you didn't create any object yet"?
@amit I am thinking he might be using an IDE which would pre-process the syntax since he might have put the statements outside of a method.
@skynorth: I don't think so, I think that the fact that the statements are not in a method - caused the compilation error - as expected.
@amit the fact that he got unexpected token compilation error tells me that he is using a custom IDE (and not even real Java, see his above comment). The Java compiler would never give that kind of compilation error.
0

All the Squares in board are null and you're trying to access a field of a null Object...

You can initialize the array with:

for(int i = 0; i < board.length; i++)
    board[i] = new Square(...something_here...);

Also, I'm not sure what you're trying to do, but you should consider using a Square[][]!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.