2012-04-22 20 views
-2

私の目標は、スタックを使用して迷路をたどることですが、遠くには到達できません。2D配列を使用した場合のNullPointerException

私はRoomの2D配列を持ち、私は常に1,1の位置から開始します。私はすべてが正しく設定されていると信じています。しかし、配列に格納されたデータにアクセスしようとすると、私はNullPointerExceptionを取得し続けます。

正しい方向に私を指し示す援助は感謝します。ここで

は私の部屋のクラスである:ここで

import java.awt.Point; 


public class Room { 
private Room up; 
private Room down; 
private Room left; 
private Room right; 
private char value; 
private boolean blocked; 
private boolean visited = false; 
private Point p; 

public void setCord(int row, int column) { 
p = new Point(row, column); 
} 

public void setUp(Room [][] r, int row, int column) { 
up = r[row][column];  
} 


public void setDown(Room[][] r, int row, int column) { 
down = r[row][column]; 

} 

public void setRight(Room[][] r, int row, int column) { 

right = r[row][column]; 
} 

public void setLeft(Room[][] r, int row, int column) { 

left = r[row][column]; 
} 

public void setValue(char c) { 

value = c; 
} 

public void setVisited(boolean b) { 
visited = b; 
} 
public void setBlocked(boolean b) { 
blocked = b; 
} 

public Point getCord() { 
return p; 
} 

public Room getUp() { 
return up; 
} 


public Room getDown() { 
return down; 
} 

public Room getRight() { 

return right; 
} 

public Room getLeft() { 

return left; 
} 

public char getValue() { 

return value; 
} 
public boolean getVisited() { 
return visited; 
} 

public boolean getBlocked() { 
return blocked; 
} 


} 

は私の迷路クラスです:

import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.PrintWriter; 
import java.util.*; 

import javax.swing.JOptionPane; 


public class Maze { 
String inFile,    // Name of file to be used as input 
     outFile,    // Name of file to output completed maze to 
     line;    // Current line being read by scanner 
    char [][] mazeContent; 
    Room [][] rooms;// Holds the values that create maze 
    Room [] theStack; 
    Room current = new Room(); 
    ArrayList<Room> al; 
    int rows, columns; 
    int tos = 0; 
    char [][] mazeC; 

    public static void main(String []args) throws Exception { 
    Maze m = new Maze(); 
    } 

    public Maze() throws FileNotFoundException { 
     // Prompts user for the name of the file they wish to use as the input file. 
     inFile = JOptionPane.showInputDialog(null, "Please enter the name of the file you wish to read, including " + 
     "the file path:"); 
     //if(inFile.equals("")) inFile = "C:\Java\JavaFiles\maze1.txt; 
     // Prompts user to enter the name they wish to save the file under. 
     outFile = JOptionPane.showInputDialog(null, "Please enter the filename you wish to save the data to:"); 
     // Creates a scanner object to read in the input file. 
     Scanner readFile = new Scanner(new FileReader(inFile)); 
     PrintWriter output = new PrintWriter(outFile); 
     rows = readFile.nextInt(); 
     columns = readFile.nextInt(); 
     readFile.nextLine(); 
     theStack = new Room[1000]; 
     mazeContent = new char [rows][columns]; 
     rooms = new Room [rows][columns]; 
     theStack = new Room[1000]; 

     for(int i = 0; i < rows; i++) { 
     line = readFile.nextLine(); 
     for(int j = 0; j< line.length(); j++) { 
     mazeContent[i][j] = line.charAt(j);   
     } 
     } 

     createRooms(); 
     findPath(); 
    } 


    private void findPath() { 
    Room start = rooms[1][1]; 
    push(start); 
     while(!isEmpty()) { 
    current = pop(); 
    //System.out.println("The value is " + current.getValue()); 
    if(current.getValue() == '$') { 
     System.out.println("Success"); 
    } 
    else if(current.getBlocked() != true && current.getVisited() != true) { 
       current.setVisited(true); 
       push(current.getRight()); 
       push(current.getLeft()); 
       push(current.getUp()); 
       push(current.getDown()); 
    } 
    } 
    } 

    public void createRooms() { 
    for(int i = 1; i < rows - 1; i++) { 
    for(int j = 1; j < columns -1; j++) { 
       Room r = new Room(); 
       r.setCord(i,j); 
       r.setValue(mazeContent[i][j]); 
       r.setUp(rooms, i-1, j); 
       r.setDown(rooms, i+1, j); 
       r.setRight(rooms, i, j+1); 
       r.setLeft(rooms, i, j-1); 
       if(mazeContent[i][j] == '*') 
        r.setBlocked(true); 
       else 
        r.setBlocked(false); 
       rooms[i][j] = r; 
    } 
    } 
    } 

    private Room pop() { 
    return theStack[--tos]; 
    } 

    private boolean isEmpty() { 
    // TODO Auto-generated method stub 
    return tos == 0; 
    } 

    private void push(Room item) { 
    if (isFull()) { 
    System.out.println("The stack is full!"); 

    } 
    else 
     theStack[tos++] = item; 
    } 

    private boolean isFull() { 

    return tos == theStack.length-1; 
    } 

} 
+3

投稿を投稿してください – Lucas

+0

よろしくお願いします。NPEを取得するにはどうすればいいですか –

+1

上記のコードを解読する人はほとんどいません。しかし、 "nullポインタ"例外が発生するのは、決して設定しなかったか、明示的にnullに設定しているために、オブジェクト参照(ポインタ)がnullであることを参照しているからです。少しのデバッグでは、オブジェクト参照がnullであることがわかり、そこから後方に作業することができます。ここにあなたのためにあなたの仕事をする人はほとんどいません。 –

答えて

1

あなたNullPointerExceptionが最も可能性の高い根本原因は、あなたが(完全に)何かを初期化していないことです。おそらくあなたのオブジェクトの1つのフィールド。おそらくあなたの配列のonの要素。この初期化されていないフィールドまたは配列要素を使用しようとすると、実際にはnull参照で操作を実行しようとしていますが、例外が発生します。


例外はcurrentnullであることを意味し、その後

if(current.getValue() == '$') 

によってスローされている場合。つまり、スタックからnullが「ポップ」されたことになります。一見すると、スタック操作の実装は正常に見えるので、私が推測するところでは、nullを押したことがあります。

nullをプッシュしようとすると、例外をスローするテストをpushメソッドに追加することをお勧めします。 (または、デバッガを使ってこれを試してみてください。)そして、逆順を続けて、nullがどこから来たのかを調べてください。

+0

ああ。それはまさに問題です。ありがとう。 – user994602

+0

@ user994602あなたのために働く場合、答えを受け入れます。 – Nikhar

関連する問題