joey101937 joey101937 - 2 months ago 7
Java Question

java 2d array not populating correctly

Overview:
i have a 2D array of "Nodes", which is a simple object i have that has a x and y coordinate and stores some basic values.

Grid is a class that holds all my nodes in its 2D node array, "nodes". the grid constructor takes two parameters: width and height (x&y) and then populates the 2D array with nodes who's coordinates(field) match their coordinates in the 2D array..... except that doest happen. for some reason, the array fills with null objects and throws a

NullPointerException
whenever i try to referance them

public class Node {

//fields

public int x, y; //coordinates
public int Fcost, Hcost; //values used for pathfinding. f is distance from user, h is distance from target.
public boolean validity = false;

//Constructors
public Node(int x, int y) {
this.x = x;
this.y = y;
}

public Node(int x, int y, int F, int H) {
this.x = x;
this.y = y;
this.Fcost = F;
this.Hcost = H;
}

public Node(Node n) {
this.x = n.x;
this.y = n.y;
this.Fcost = n.Fcost;
this.Hcost = n.Hcost;
}



public boolean isValid() {
////if out of bounds, return flase.
if (this.x >= Game.width) {
return false;
}
if (this.x < 0) {
return false;
}
if (this.y >= Game.height) {
return false;
}
if (this.y < 0) {
return false;
}

return true;
}

public void checkIfValid() {
this.validity = this.isValid();
}





public class Grid {

public Node[][] nodes;
private int length, height;

///constructor
//populates the grid with a new node for each coordinate
public Grid(int x, int y) {
nodes = new Node[x + 1][y + 1];
for (int i = 0; i < x; i++) {
for (int w = 0; w < y; w++) {
nodes[x][y] = new Node(x, y);
System.out.println("populating...");
}
}
this.length = x;
this.height = y;

////prints the number of nodes
int w = 0;
for (Node[] a : nodes) {
for (Node n : a) {
w++;
}
}
System.out.println("nodes " + w);
}

///methods
public Node[] getNeighbors(Node in) {
ArrayList<Node> n = new ArrayList<>();

////NOT YET IMPLEMENTED
return (Node[]) n.toArray();
}

///tells each node to check weather or not it is valid
public void update() {
for (Node n[] : nodes) {
for (Node realNode : n) {
realNode.checkIfValid();
}
}
}

}


EDIT-
here is what prints out.
Game is the class that calls the "update" method for its grid.

java.lang.NullPointerException
at Pathfinding.Grid.update(Grid.java:55)
at pkg2dgame.Game.tick(Game.java:62)
at pkg2dgame.Game.run(Game.java:104)
at java.lang.Thread.run(Thread.java:745)

Answer

nodes[x][y] = new Node(x, y); should be nodes[i][w] = new Node(x, y);

You are repopulating the same index all the time due to which all the array buckets are null. One more concern with your code is that, for loop doesn't loop till the end of the 2d array.

for (int i = 0; i < nodes.length; i++) {
        for (int w = 0; w < nodes[i].length; w++) {
            nodes[i][w] = new Node(x, y);
            System.out.println("populating...");
        }
}

It is throwing that error as operations are being performed on null value.

Comments