簡體   English   中英

java 2d數組沒有正確填充

[英]java 2d array not populating correctly

概述:我有一個“節點”的2D數組,這是一個簡單的對象,我有ax和y坐標,並存儲一些基本值。

Grid是一個類,它將我的所有節點保存在其2D節點數組中,即“節點”。 網格構造函數采用兩個參數:寬度和高度(x和y),然后使用其坐標(字段)匹配其在2D數組中的坐標的節點填充2D數組.....除了doest發生。 由於某種原因,每當我嘗試重新引用它們時,數組都會填充空對象並拋出NullPointerException

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();
        }
    }
}

}

編輯 - 這是打印出來的。 游戲是為其網格調用“更新”方法的類。

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)

正確填充節點

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

nodes[x][y] = new Node(x, y); 應該是nodes[i][w] = new Node(x, y);

您正在重新填充相同的索引,因為所有數組存儲桶都為空。 您的代碼的另一個問題是,for循環不會循環直到2d數組的末尾。

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...");
        }
}

正在對null值執行操作時拋出該錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM