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.
Coordsof theCoords[]array?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.