0

I am trying to check the state of cells for a true value. If the state is true, then I am going to save the X and Y of the element (2D Array). Here is the method I am using where c is the counter that counts how many cells are true:

public void checkState(){
        System.out.println("Checking State");
        int c = 0;

        for(int y = 0; y < GRID_Y; y++){
            for(int x = 0;x < GRID_X; x++){
                if(state[x][y]==true){
                    //Error starts after this line
                    coords[c].setX(x);
                    coords[c].setY(y);
                    c++;

                    //DEBUG

                        System.out.println(coords.getX());
                        System.out.println(coords.getY());
                }

            }
        }
        System.out.println(c);
    }
}

The coords object is declared like this:

private Coords[] coords = new Coords[totalCells];

(Tried changing totalCells to a small integer value like 10, still didn't work)

Where Coords class is:

public class Coords {
    int x,y;

    public Coords(){
        x = 0;
        y = 0;
    }

    public void setX(int xIn){
        x = xIn;
    }

    public void setY(int yIn){
        y = yIn;
    }

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

}

I am getting the following error:

Checking State
java.lang.NullPointerException
    at game.GameOfLifeGUI.checkState(GameOfLifeGUI.java:116)
    at game.GameOfLifeGUI.<init>(GameOfLifeGUI.java:57)
    at game.GameOfLifeGUI$1.run(GameOfLifeGUI.java:32)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

I don't get how a null value is being passed as I am storing the x and y of the for loops.

Thanks for your time.

3
  • Did you instantiate each Coords of the Coords[]array? Commented Apr 4, 2014 at 13:56
  • private Coords[] coords = new Coords[totalCells]; creates an array of nulls. That's why you get the NPE error. You need to fill it after the creation. Commented Apr 4, 2014 at 13:57
  • Yeah that was the problem. I had a brain fart for a moment.Thanks. Commented Apr 4, 2014 at 14:06

2 Answers 2

3

After this : private Coords[] coords = new Coords[totalCells];

Add :

for(int index = 0; index < totalCells-1; index++){
    coords[index] = new Coords();
}

That should do the trick

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

3 Comments

Yeah I always forget to do this when I am working with arrays of objects.
Your totally right. But lets hope he have a gridX*gridY == totalCell. Or he will be posting again for an ArrayOutOfBoundExecption :D
Yeah yeah I do have that :)
1

While @user3497004's answer is fine for java < 8, you should prefer:

Arrays.setAll(coords, n -> new Coords());

for java 8 (and probably above, let's be futuristic).

2 Comments

What does n represent? Never used the Arrays class tbh.
n is the index of the particular cell being initialized

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.