0

I'm currently writing a little game using an 2D Array (Array of Arrays). In one of my classes which is handling the movement and placement of enemy units, there is a method that calls upon a method called "getPos" this is the Array in which the position of all enemies are stored, however I'm encountering a NullPointerException and can't for the life of me spot why it's snagging there.

Below is the code the exception is flagged, I managed to pin the error on the line

"gridPos[0][0].getType()":

public class Movement extends ArrayControl {

public Movement(Display display) {
    super(display);
}

private static ArrayControl arrayControl = new ArrayControl(display);

public static void addEnemyShip(){
    EnemyBehaviour[][] gridPos = display.getGridPos();
    gridPos[0][0] = new Ship();
    arrayControl.updatePosition(gridPos[0][0].getType(), 0, 0);
}

And this is the segment that handles the actual passing of gridPos:

public EnemyBehaviour[][] getGridPos() {
    return gridPos;
}

And this is the getType() method in the super class of Ship()

public String getType() {
    return type;
}
Mr. Slug
  • 100
  • 1
  • 10
  • How large is the `gridPos` array? Did you try stepping your program through a debugger? – Moritz Petersen Nov 19 '17 at 11:00
  • gridPos is 4 by 4 ([4][4]) As this is the size of the gaming grid. setCoords, currently isn't working, sorry I should've cut that part out since it works without it, besides the NullPointer on gridPos – Mr. Slug Nov 19 '17 at 11:02
  • Welcome to Stack Overflow! Please take the [tour](/tour), have a look around, and read through the [help center](/help) , in particular [How do I ask a good question?](/help/how-to-ask) and [What topics can I ask about here?](/help/on-topic). Please provide [enhance]() you example to be a [Minimal, Complete, and Verifiable example (MCVE)](/help/mcve) – Timothy Truckle Nov 19 '17 at 11:03

0 Answers0