2016-09-22 8 views
1

概要: 私はxノードとy座標を持ち、基本的な値を格納している単純なオブジェクトである "ノード"の2次元配列を持っています。java 2次元配列が正しく設定されていない

グリッドは、2Dノード配列「ノード」内のすべての自分のノードを保持するクラスです。グリッドコンストラクタは2つのパラメータwidthとheight(x & y)をとり、2次元配列の座標と一致するノード(...)を持つ2次元配列にデータを取り込みます。何らかの理由で、配列がnullのオブジェクトを埋め、私はそれらをここに

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

} 

編集 - プリントアウトするものですがreferanceしようとするたびNullPointerExceptionをスローします。 ゲームは、グリッドの「更新」メソッドを呼び出すクラスです。

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) 
+0

ノードを移入あなたは私たちには、コンソールから取得したエラーメッセージを表示することができますか? –

+2

あなたはiとwを反復していますが、常に同じノードに割り当てます(ノード[x] [y]はノード[i] [w]ですか?) – HomeIsWhereThePcIs

答えて

1

nodes[x][y] = new Node(x, y);あなたは同じインデックスにすべてのアレイのバケットがNULLであるため、これにすべての時間を再増殖している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値で操作が実行されているため、このエラーが発生しています。

+0

ありがとう、ありがとう。完璧に動作する – joey101937

2

が正しく

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..."); 
     } 
    } 
関連する問題