我在CS1分配中遇到麻烦,希望您能帮助我! This is my assignment
我已经开始工作,但是运行play.java时出现Null Pointer Exception错误。我对play.java和FMP.java的代码如下
FMP.java:
public class FMP {
// ATTRIBUTES: ******************************************************************
private int[][] puzzle;
private String[][] path;
private boolean solvable;
// CONSTRUCTORS: ******************************************************************
//default constructor
//...........YOUR CODE GOES HERE
public FMP(){
}
//constructor that takes in a 2D-array of integers representing a maze
public FMP(int[][] maze) {
// Initialize puzzle
//...........YOUR CODE GOES HERE
int[][] puzzle = new int [10][10];
// Initialize path to be a table of the same dimensions as puzzle
//...........YOUR CODE GOES HERE
String[][] path = new String[10][10];
// fill path with spaces
//...........YOUR CODE GOES HERE
for(int i=0; i < 9; i++){
for(int j=0; j < 9; j++){
path[i][j] = " ";
}
}
// call the method buildPath on your current instance: given to you
this.buildPath();
}
// SETTERS: ******************************************************************
public void setPuzzle(int[][] maze) {
// Same code as for the second constructor
// copy and paste here
int[][] puzzle = new int [10][10];
String[][] path = new String[10][10];
for(int i=0; i < 9; i++){
for(int j=0; j < 9; j++){
path[i][j] = " ";
}
}
}
// GETTERS: ******************************************************************
// One getter method per attribute (lines 13, 14, and 15)
// YOUR GETTER METHODS GO HERE
public int[][] getPuzzle(){
return puzzle;
}
public String[][] getPath(){
return path;
}
public boolean getSolvable(){
return solvable;
}
// OTHER METHODS: ************************************************************
/* buildPath:
***************************************************************************/
public String[][] buildPath() {
// HERE GOES CODE FROM YOUR PREVIOUS METHOD buildPath
String[][] result = new String[puzzle.length][puzzle[0].length];
for (int i=0; i<result.length; i++) {
for (int j=0; j<result[0].length; j++) {
result[i][j]=" ";
}
}
int i = 0;
int j = 0;
result[0][0] = "X";
while (i < puzzle.length && j < puzzle[0].length) {
if (puzzle[i][j] % 2 == 0 && result[i][j - 1].equals(" ")){
j--;
result[i][j] = "X";
}
else if (puzzle[i][j] % 5 == 0 && result[i][j + 1].equals(" ")){
j++;
result[i][j] = "X";
}
else if (puzzle[i][j] % 3 == 0 && result[i - 1][j].equals(" ")){
i--;
result[i][j] = "X";
}
else if (puzzle[i][j] % 7 == 0 && result[i + 1][j].equals(" ")){
i++;
result[i][j] = "X";
}
if( i == 9 && j == 9){
break;
}
}
if ((i!=puzzle.length-1)||(j!=puzzle[0].length-1)){
solvable = false;
}
else{
solvable = true;
}
return result;
}
// At the end of your code, if you have reached the bottom right corner
// of the maze, the puzzle is solvable (true). Otherwise it is not (false).
// Your code may look like the following two commented lines:
// if ((i!=puzzle.length-1)||(j!=puzzle[0].length-1)) solvable = false;
// else solvable = true;
/* printPath:
***************************************************************************/
public void printPath() {
int nextLine = 0;
System.out.println("_______________________________");
for( int i = 0; i < path.length; i++){
for ( int j = 0; j < path[0].length; j++){
System.out.print("| " + path[i][j]);
nextLine++;
if( nextLine % 10 == 0){
System.out.println("|");
System.out.println("_______________________________");
}
}
}
}
}
play.java:
public class play {
public static void main(String[] args) {
int[][] puzzle = {{225, 200, 70, 14, 35, 14, 35, 14, 25, 14},
{35, 40, 6, 15, 28, 15, 18, 15, 20, 42},
{75, 20, 14, 35, 40, 50, 10, 14, 5, 42},
{35, 14, 21, 21, 35, 200, 2, 105, 14, 21},
{21, 15, 12, 225, 49, 35, 14, 9, 21, 9},
{115, 50, 20, 40, 77, 9, 15, 14, 225, 14},
{28, 35, 200, 14, 35, 20, 50, 14, 35, 6},
{21, 3, 35, 42, 49, 20, 20, 14, 105, 14},
{15, 50, 28, 21, 5, 70, 14, 21, 9, 21},
{5, 40, 205, 300, 40, 25, 15, 45, 20, 42}};
FMP p1 = new FMP(puzzle);
p1.printPath();
}
}
play.java不需要修改,只有FMP.java。 为什么会出现Null指针异常?
答案 0 :(得分:0)
问题是您在FMP(int[][])
构造函数中正在屏蔽您的字段。这个
int[][] puzzle = new int [10][10];
// Initialize path to be a table of the same dimensions as puzzle
//...........YOUR CODE GOES HERE
String[][] path = new String[10][10];
应该是
this.puzzle = new int [10][10];
this.path = new String[10][10];