-2

私の目標は、スタックを使用して迷路を横断することですが、遠くまで行くことができません。

オブジェクトの 2D 配列があり、Room常に位置 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;
    }

}
4

1 に答える 1

1

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


例外がスローされている場合

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

つまり、それcurrentは ですnull。これは、スタックから a を「ポップ」したことを意味しますnull。一見すると、スタック操作の実装は問題ないように見えるので、どこかにnull.

push私の提案は、プッシュしようとすると例外をスローするテストをメソッドに追加することnullです。(または、デバッガーを使用してこれを追跡してみてください。) 次に、逆方向に作業を続けて、 がどこnullから来たのかを突き止めます。

于 2012-04-22T00:36:55.997 に答える