我想給一個叫做State的類的每個子項賦值,而我有一個初始狀態爲空的狀態數組,我在這裏接收空指針引用:當初始化一個空值項時出現空指針異常
//finding all the neighbor states of a given configuration
public State[] neighborStates(String config, int modeFlag){
State[] neighborStates=new State[7];
int i=0;
for (Operation o : Operation.values()){
neighborStates[i].config=move(config,o.name().charAt(0));
neighborStates[i].realCost++;
neighborStates[i].opSequence+=o.name();
neighborStates[i].heuristicCost=getHeuristicCost(neighborStates[i].config, modeFlag);
i++;
}
return neighborStates;
}
我改變了代碼,但我還沒有得到NPE:
public State[] neighborStates(String config, int modeFlag){
State[] neighborStates=new State[8];
int i=0;
for (Operation o : Operation.values()){
neighborStates[i] = new State(move(config,o.name().charAt(0)),neighborStates[i].realCost++,
getHeuristicCost(neighborStates[i].config, modeFlag), neighborStates[i].opSequence+=o.name());
//neighborStates[i].config=move(config,o.name().charAt(0));
//neighborStates[i].realCost++;
//neighborStates[i].opSequence+=o.name();
//neighborStates[i].heuristicCost=getHeuristicCost(neighborStates[i].config, modeFlag);
i++;
}
類國家的定義是:
public class State {
public State(String config, int realCost, int heuristicCost, String opSequence){
this.config = config;
this.realCost = realCost;
this.heuristicCost = heuristicCost;
this.opSequence = opSequence;
}
嗨艾略特,我更新了你說的代碼,但我還沒有得到NPE,請你看看? – 2014-11-25 03:32:26
這不是我說的,你的「國家」從非構造者變成了一切?因爲State中的構造函數是 – 2014-11-25 03:34:24
。java就像'public State(String config,int realCost,int heuristicCost,String opSequence)' – 2014-11-25 03:35:10