English 中文(简体)
使用2D阵列时的Null PointerException
原标题:NullPointerException when using 2D array

我的目标是用一 st子verse笑一个马奇,但我无法走得很远。

我有2D系列的<代码>Room物体,我总是从第1位开始。 我认为,我所建立的一切都是正确的。 然而,每当我试图查阅我阵列中储存的数据时,我就不断收到<条码>。

任何将给我指明正确方向的援助都会受到赞赏。

我的房间:

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:JavaJavaFilesmaze1.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;
    }

}
问题回答

贵岛最可能的根源是,你没有(完全)做些什么。 或许是你们的目标之一。 或许是你阵列中的一个要素。 当你试图利用这一未初衷的实地或阵列要素时,你实际上试图在<>null参引栏上开展一项行动,并造成例外情况。


如果该例外被推翻,

    if(current.getValue() ==  $ )

也就是说,<代码>的现行为<代码>null。 这意味着,你在座标上“填满”。 从一看,执行你的分管行动就看着科索沃,因此,我的猜测是,在有些地方,你推行了>>。

我的建议是在<代码>push上添加一个测试。 如果你试图推介<代码>null<>/code>,则放弃例外情况的方法。 (Or试图用夸张的方式追踪这一下降。) 然后继续工作,以图显示<代码>null源自何处。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签