0
我正在編寫一個謎題程序。當我編譯我的Java程序時,它是成功的。但是當我運行它時,它顯示廣度優先搜索java.lang.NullPointerException
Solution to problem using breadth first :
Exception in thread "main" java.lang.NullPointerException
at SolvingProblem.isGoal(SolvingProblem.java:24)
at AbstractTreeSearch.solve(AbstractTreeSearch.java:31)
at EightP.main(EightP.java:15)
我花了幾個小時並修復代碼但未成功。理想情況下,它應該顯示3x3陣列配置。任何人都可以幫助我並指出問題所在?
State initialState = new State(State.arrayA);
State GoalState = new State(State.arrayG);
@Override
public Object getInitialState() {
return initialState;
}
@Override
public boolean isGoal(Object state) {
return state.equals(GoalState);
}
下面
public Node solve(Problem problem) {
//initialize the search tree using the initial state of problem
frontier = initFrontier();
frontier.addAll(expand(new Node(problem.getInitialState()), problem));
//Starting frontier
boolean done = false;
Node solution = null;
while (!done) {
if (frontier.isEmpty()) {
System.out.println("Blank frontier");
done = true;
} else {
Node node = chooseLeafNode(frontier, problem);
//inspecting node
if (problem.isGoal(node.getState())) {
System.out.println("Solution found");
System.out.println();
solution = node;
done = true;
} else {
//Expanding node, frontier is..
frontier.addAll(expand(node, problem));
}
}
}
嗨xim,謝謝。這是有用的建議。我解決它,發現我的chooseleafnode()沒有返回任何值。因此爲空。然後它會導致我在另一個類中進行變量初始化。它與2D數組無法將值傳遞給chooseleafnode()有關。我改回1D陣列,它的工作原理。 – lowerbound