我想要如在上述代碼getNumber如下的NullPointerException而在2維陣列訪問方法,在一個對象中
public static void main(String[] args) throws IOException {
length=getNumber("Enter the length of the field: ");
breadth=getNumber("Enter the breadth of the filed: ");
node n = new node();
node [][] field = new node[length][breadth];
for(i=0;i<=length;i++){
for(j=0;j<=breadth;j++){
F =getNumber("Enter the F value");
field[i][j].setF(F);
System.out.println(" "+field[i][j].getF(F);
}
}
}
製造「節點」的對象的2維陣列是一個函數式中,i和打印接受數 這裏是我的節點類:
public class node {
public int F;
public int G;
public int H;
public boolean isVisited;
public boolean isCurrent;
public void node(int F,int G,int H,boolean isVisited, boolean isCurrent){
this.F=F;
this.G=G;
this.H=H;
this.isVisited=isVisited;
this.isCurrent=isCurrent;
}
public int getF() {
return G+H;
}
public void setF(int f) {
F = f;
}
public int getG() {
return G;
}
public void setG(int g) {
G = g;
}
public int getH() {
return H;
}
public void setH(int h) {
H = h;
}
public boolean isVisited() {
return isVisited;
}
public void setVisited(boolean isVisited) {
this.isVisited = isVisited;
}
public boolean isCurrent() {
return isCurrent;
}
public void setCurrent(boolean isCurrent) {
this.isCurrent = isCurrent;
}
}
所有我想要做的是,存儲/每一F,G,H等的接入各種值的節點對象,但問題是我得到java.lang.NullPointerException
爲field[i][j].setF(F);
我不知道我要去哪裏錯了,需要一些幫助。
我該如何填充它?因爲它的對象數組(我是一個初學者如此詳細地闡述) – md1hunox 2012-04-03 13:03:10
@vineetrok檢查我的答案的底部,在那裏我詳細說明。 – hvgotcodes 2012-04-03 13:04:45
非常感謝,現在就在工作 – md1hunox 2012-04-03 13:11:35