2014-10-17 3 views
-3

compareTo 메소드를 구현하는 노드 initialState를 추가하려고 할 때 왜 null 포인터를 얻는 지 모르겠습니다. 다음은 내 Node 클래스입니다. 내가 초기 상태 (Initial)를 추가 해요 때 예외가Java PriorityQueue 및 NullPointerException

private void Asearch() { 
    openListP = new PriorityQueue<Node>(1000); 
    closedList = new LinkedList<Node>(); 
    initialState.setF(); 
    //print(initialState); 
    openList.add(initialState); // initial state into open list 
    while (!openList.isEmpty()){ 
     Node n = openListP.remove(); 
     if (isGoal(n.state)){      // check if goal 
      printPath(n); 
      return; 
     } 
     else { 
     expandH(n);  // expand to next frontier 
     closedList.add(n); 
     } 
    } 
    if(openList.isEmpty()) 
     System.out.println(" unsolvable"); 

} 

을 발생하는 위치 여기

package puzzle; 

public class Node implements Comparable<Node> { 

int[][] state; 
Node parent; 
String action; 
Node up, down, left, right; 
int r,c; 
int f; 

Node(int[][] s, Node n,int row, int column){ 
    state = s; 
    parent = n; 
    up = null; 
    down = null; 
    left = null; 
    right = null; 
    r = row; 
    c = column; 
    f = 0; 
} 

public void setF() { 
    int g = 0; // distance 
    int h = 0; // Manhattan distance 
    int k = 0; 
    for (int i = 0; i < state.length; i++){ 
     for (int j = 0; j < state[i].length; j++){ 
      if (state[i][j] != k) 
       g++; 
      k++;  
     } 
    } 
    // http://stackoverflow.com/questions/12526792/manhattan-distance-in-a 
    for (int x = 0; x < state.length; x++) {  // x-dimension, traversing rows (i) 
     for (int y = 0; y < state[x].length; y++) { // y-dimension, traversing cols (j) 
      int value = state[x][y];     // tiles array contains board elements 
      if (value != 0) {       // we don't compute MD for element 0 
       int targetX = (value)/state.length; // expected x-coordinate (row) 
       int targetY = (value) % state.length; // expected y-coordinate (col) 
       int dx = x - targetX;     // x-distance to expected coordinate 
       int dy = y - targetY;     // y-distance to expected coordinate 
       h += Math.abs(dx) + Math.abs(dy); 
      } 
     } 
    } 
    for(int i = 0; i < state.length; i++){ 
     for (int j = 0; j < state.length; j++){ 
      System.out.print(state[i][j]); 
     } 
     System.out.println(); 
    } 
    System.out.println(h); 
    System.out.println(g); 
    f = h + g; 
} 


    @Override 
    public int compareTo(Node node) { 
     if (this.f < node.f) 
      return -1; 
     else if (this.f > node.f) 
      return 1; 
     else return 0; 

    } 
} 

하고 난 당신이 openListP에 openList을 변경해야이 오류

Exception in thread "main" java.lang.NullPointerException 
at puzzle.Puzzle.Asearch(Puzzle.java:150) 
at puzzle.Puzzle.solve(Puzzle.java:142) 
at puzzle.Test.main(Test.java:11) 
+0

어떻게 당신이 당신의 초기 상태 (Initial)를 초기화하는? –

+0

initialState가 이미 초기화되었고 print 메소드와 setF 메소드를 사용하여 테스트했습니다. –

+0

openList가 시작 되었습니까? openList를 초기화하지만 openList가 초기화 된 것을 볼 수 없습니다. – rakke

답변

0

를 얻을 수있다.

openList.add(initialState); // initial state into open list 

는해야

openListP.add(initialState); // initial state into open list