[英]Writing a Java program to run with a pre-built interface (the Life game) but I'm having trouble
这是我的代码: http : //pastebin.com/umy0FPvB (LG)
这是老师的代码: http : //pastebin.com/y5wU0Zpx (LCI)
这告诉我,当LCI试图从LG [world()]传递的矩阵中读取数据时,我在老师代码的第41行上错了。
我已经坐了一段时间了,但我似乎无法弄清楚出了什么问题。
Exception in thread "main" java.lang.NullPointerException
at Console.printWorld(Console.java:41)
at Console.playLife(Console.java:56)
at Console.main(Console.java:30)
-
/**
* The Life game
* @author Noah Kissinger
* @date 2012.2.13
*/
import java.util.Random;
public class Life {
private static boolean[][] matrix;
private static int bL, bH, lL, lH, r, c;
private static long rSeed;
public Life(long seed, int rows, int columns, int birthLow, int birthHigh,
int liveLow, int liveHigh) {
rSeed = seed;
bL = birthLow;
bH = birthHigh;
lL = liveLow;
lH = liveHigh;
r = rows;
c = columns;
createMatrix();
}
public void update() {
updateMatrix();
}
public boolean[][] world() {
return matrix;
}
public static void createMatrix() {
Random seedBool = new Random(rSeed);
boolean[][] matrix = new boolean[r][c];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = false;
}
}
for (int i = 1; i < matrix.length - 1; i++) {
for (int j = 1; j < matrix[i].length - 1; j++) {
matrix[i][j] = seedBool.nextBoolean();
}
}
}
public static void updateMatrix() {
Random seedBool = new Random(rSeed);
boolean[][] matrixCopy = matrix.clone();
for (int i = 0; i < matrix.length; i++)
matrixCopy[i] = matrix[i].clone();
int count = 0;
for (int i = 1; i < matrix.length - 1; i++) {
for (int j = 1; j < matrix[i].length - 1; j++) {
if (matrix[i][j] == false) {
if (matrixCopy[i - 1][j - 1] == true)
count++;
if (matrixCopy[i - 1][j] == true)
count++;
if (matrixCopy[i - 1][j + 1] == true)
count++;
if (matrixCopy[i][j - 1] == true)
count++;
if (matrixCopy[i][j + 1] == true)
count++;
if (matrixCopy[i + 1][j - 1] == true)
count++;
if (matrixCopy[i + 1][j] == true)
count++;
if (matrixCopy[i + 1][j + 1] == true)
count++;
if (count >= bL && count <= bH) {
matrix[i][j] = true;
for (int i1 = 0; i1 < matrix.length; i1++) {
for (int j1 = 0; j1 < matrix[i1].length; j1++) {
matrix[i1][j1] = false;
}
}
for (int i1 = 1; i1 < matrix.length - 1; i1++) {
for (int j1 = 1; j1 < matrix[i1].length - 1; j1++) {
matrix[i1][j1] = seedBool.nextBoolean();
}
}
} else
matrix[i][j] = false;
count = 0;
}
else {
if (matrixCopy[i - 1][j - 1] == true)
count++;
if (matrixCopy[i - 1][j] == true)
count++;
if (matrixCopy[i - 1][j + 1] == true)
count++;
if (matrixCopy[i][j - 1] == true)
count++;
if (matrixCopy[i][j + 1] == true)
count++;
if (matrixCopy[i + 1][j - 1] == true)
count++;
if (matrixCopy[i + 1][j] == true)
count++;
if (matrixCopy[i + 1][j + 1] == true)
count++;
if (count >= lL && count <= lH)
matrix[i][j] = true;
else
matrix[i][j] = false;
count = 0;
}
}
}
}
}
-
/**
* The Console class is a console interface to the Life game.
* @author DH
* @date Sept. 2008
*/
import java.util.Scanner;
public class Console {
/**
* @param args unused
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter the size of the matrix(rows, columns) :");
int rows = in.nextInt();
int columns = in.nextInt();
System.out.println("Please enter random seed: ");
long seed = in.nextLong();
System.out.println("Please enter birth range (low, high) :");
int birthLow = in.nextInt();
int birthHigh = in.nextInt();
System.out.println("Please enter live range (low, high): ");
int liveLow = in.nextInt();
int liveHigh = in.nextInt();
try {
Life game = new Life(seed, rows, columns, birthLow, birthHigh, liveLow, liveHigh);
playLife(game);
} catch (IllegalArgumentException e) {
System.out.println("Inappropriate values: " + e.getMessage());
}
}
/**
* Print a boolean matrix
* @param world is a boolean matrix to be printed with # for true and - for false.
*/
public static void printWorld(boolean[][] matrix) {
for (int r=0; r<matrix.length; r++) {
for (int c=0; c<matrix[0].length; c++) {
System.out.print(matrix[r][c] ? " # " : " - ");
}
System.out.println();
}
System.out.println();
}
/**
* Play the game of Life starting with a given state
* @param game is the Life object that provides the current state of Life
*/
public static void playLife(Life game) {
printWorld(game.world());
for (int i=0; i<10; i++) {
game.update();
printWorld(game.world());
}
}
}
这是你的问题。 在您的createMatrix()
方法可以定义一个局部变量matrix
,当你真的要修改字段matrix
。
您可能会发现使用this
访问字段很有用,例如this.matrix
。 它在代码中有明显的区别。 但是,大多数IDE都会自动突出显示字段和局部变量,因此有些人认为它是不必要的,这是样式问题,并不是太重要。
我没有检查您程序的其余部分,可能还有其他错误。
public static void createMatrix() {
Random seedBool = new Random(rSeed);
this.matrix = new boolean[r][c];
for (int i = 0; i < this.matrix.length; i++) {
for (int j = 0; j < this.matrix[i].length; j++) {
this.matrix[i][j] = false;
}
}
for (int i = 1; i < this.matrix.length - 1; i++) {
for (int j = 1; j < this.matrix[i].length - 1; j++) {
this.matrix[i][j] = seedBool.nextBoolean();
}
}
}
boolean[][] matrix = new boolean[r][c];
这行代码创建一个二维布尔数组,并将其存储在createMatrix
方法的loca变量中。 因此,Life类中的静态字段matrix
仍然为null
。 读取该字段,并将其通过world
方法传递给playLife
方法。 然后,接下来调用printLife
方法触发NPE。
顺便说一句,为什么您要使用许多静态字段和方法来实现生活游戏?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.