-2

我的目标是使用堆栈穿越迷宫,但我无法走得很远。

我有一个二维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 最可能的根本原因是您没有(完全)初始化某些东西。也许你的一个对象的一个​​领域。也许你的数组中的一个元素。然后,当您尝试使用此未初始化的字段或数组元素时,您实际上是在尝试对null引用执行操作......这会导致异常。


如果异常被抛出

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

那么这意味着那currentnull。这意味着您null从堆栈中“弹出” a 。乍一看,你的堆栈操作的实现看起来不错,所以我猜是你在某个地方推送了一个null.

我的建议是在push尝试 push 时在引发异常的方法中添加一个测试null。(或者尝试使用调试器进行跟踪。)然后继续向后工作以找出null来源。

于 2012-04-22T00:36:55.997 回答