使用 SelectInput() 时出现 NullPointer 异常
NullPointer Exception When Using SelectInput()
我正在用 Processing 3.x 编写游戏,我正在尝试使用 SelectInput();一种加载不同游戏保存文件的方法。但是,我不断收到无法修复的 NullPointer 异常。我一直尝试在 loadGame 函数之前 运行 SelectInput 函数,但程序还是崩溃了。目前,在下面的代码中,程序在读作 'columns=splitTokens(rows[1]);'
的行出错
如果能帮助此代码再次运行,我们将不胜感激。
代码:
PImage tile1;
PImage tile2;
PImage tile3;
PImage tile4;
PImage tile5;
PImage tile6;
PImage tile7;
PImage tile8;
String input;
String rows[];
String columns[];
int array2D[][];
boolean gameLoaded = false;
void setup() {
size(600, 600);
background(0);
frameRate(30);
tile1 = loadImage("grass0.png");
tile2 = loadImage("grass1.png");
tile3 = loadImage("grass2.png");
tile4 = loadImage("tile1.png");
tile5 = loadImage("tile2.png");
tile6 = loadImage("tree.png");
tile7 = loadImage("tree2.png");
tile8 = loadImage("tile2.png");
selectInput("Select a file to process:", "fileSelected");
if (gameLoaded == false) {
//selectInput("Select a file to process:", "fileSelected");
loadGame();
gameLoaded = true;
}
}
void draw() {
if (gameLoaded == true) {
for (int a = 0; a < rows.length; a++) {
for (int b = 0; b < columns.length; b++) {
if (array2D[a][b] == 1) {
image(tile1, a * 100, b * 100);
}
if (array2D[a][b] == 2) {
image(tile2, a * 100, b * 100);
}
if (array2D[a][b] == 3) {
image(tile3, a * 100, b * 100);
}
if (array2D[a][b] == 4) {
image(tile4, a * 100, b * 100);
}
if (array2D[a][b] == 5) {
image(tile5, a * 100, b * 100);
}
if (array2D[a][b] == 6) {
image(tile6, a * 100, b * 100);
}
if (array2D[a][b] == 7) {
image(tile7, a * 100, b * 100);
}
if (array2D[a][b] == 8) {
image(tile8, a * 100, b * 100);
}
}
}
}
}
void loadGame() {
//rows=loadStrings("file.txt");
rows = loadStrings(input);
columns = splitTokens(rows[1]);
array2D = new int[rows.length][columns.length];
println("There are " + rows.length + " rows");
println("There are " + columns.length + " columns");
for (int a = 0; a < rows.length; a++) {
columns = splitTokens(rows[a]);
for (int b = 0; b < columns.length; b++) {
array2D[a][b] = Integer.parseInt(columns[b]);
}
}
}
void fileSelelected(File selection) {
if (selection == null) {
println("Nothing was selected, so nothing happens");
} else {
input = selection.getAbsolutePath();
//rows=loadStrings(input);
}
}
阅读起来很容易,但 documentation 建议将文件 select 或 运行 放在单独的线程上。这意味着您的程序不会等到文件被 selected 并且 select 输入函数完成。相反,它会继续处理您的代码,直到 selected 文件,然后中断正常处理以处理 fileSelected 函数。
在您的代码中,这意味着 loadGame()
总是 运行 在您有机会 select 文件之前。解决方案是创建一个变量来检查文件是否已被 selected。
在代码的顶部创建变量:
boolean isFileSelected = false;
加载游戏前,检查文件是否selected:
if (gameLoaded == false && isFileSelected == true) {
loadGame();
gameLoaded = true;
}
select编辑文件时,更改变量:
void fileSelelected(File selection) {
if (selection == null) {
println("Nothing was selected, so nothing happens");
} else {
input = selection.getAbsolutePath();
isFileSelected = true;
}
}
如果还不是很清楚,下面的脚本可以帮助理解这个概念。一旦程序启动,它将继续打印 start
,直到您 select 编辑了一个文件。
String test = "start";
void setup() {
selectInput("Select a file to process:", "fileSelected");
}
void draw(){
println(test);
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
println("User selected " + selection.getAbsolutePath());
test = "test";
}
}
我正在用 Processing 3.x 编写游戏,我正在尝试使用 SelectInput();一种加载不同游戏保存文件的方法。但是,我不断收到无法修复的 NullPointer 异常。我一直尝试在 loadGame 函数之前 运行 SelectInput 函数,但程序还是崩溃了。目前,在下面的代码中,程序在读作 'columns=splitTokens(rows[1]);'
的行出错如果能帮助此代码再次运行,我们将不胜感激。
代码:
PImage tile1;
PImage tile2;
PImage tile3;
PImage tile4;
PImage tile5;
PImage tile6;
PImage tile7;
PImage tile8;
String input;
String rows[];
String columns[];
int array2D[][];
boolean gameLoaded = false;
void setup() {
size(600, 600);
background(0);
frameRate(30);
tile1 = loadImage("grass0.png");
tile2 = loadImage("grass1.png");
tile3 = loadImage("grass2.png");
tile4 = loadImage("tile1.png");
tile5 = loadImage("tile2.png");
tile6 = loadImage("tree.png");
tile7 = loadImage("tree2.png");
tile8 = loadImage("tile2.png");
selectInput("Select a file to process:", "fileSelected");
if (gameLoaded == false) {
//selectInput("Select a file to process:", "fileSelected");
loadGame();
gameLoaded = true;
}
}
void draw() {
if (gameLoaded == true) {
for (int a = 0; a < rows.length; a++) {
for (int b = 0; b < columns.length; b++) {
if (array2D[a][b] == 1) {
image(tile1, a * 100, b * 100);
}
if (array2D[a][b] == 2) {
image(tile2, a * 100, b * 100);
}
if (array2D[a][b] == 3) {
image(tile3, a * 100, b * 100);
}
if (array2D[a][b] == 4) {
image(tile4, a * 100, b * 100);
}
if (array2D[a][b] == 5) {
image(tile5, a * 100, b * 100);
}
if (array2D[a][b] == 6) {
image(tile6, a * 100, b * 100);
}
if (array2D[a][b] == 7) {
image(tile7, a * 100, b * 100);
}
if (array2D[a][b] == 8) {
image(tile8, a * 100, b * 100);
}
}
}
}
}
void loadGame() {
//rows=loadStrings("file.txt");
rows = loadStrings(input);
columns = splitTokens(rows[1]);
array2D = new int[rows.length][columns.length];
println("There are " + rows.length + " rows");
println("There are " + columns.length + " columns");
for (int a = 0; a < rows.length; a++) {
columns = splitTokens(rows[a]);
for (int b = 0; b < columns.length; b++) {
array2D[a][b] = Integer.parseInt(columns[b]);
}
}
}
void fileSelelected(File selection) {
if (selection == null) {
println("Nothing was selected, so nothing happens");
} else {
input = selection.getAbsolutePath();
//rows=loadStrings(input);
}
}
阅读起来很容易,但 documentation 建议将文件 select 或 运行 放在单独的线程上。这意味着您的程序不会等到文件被 selected 并且 select 输入函数完成。相反,它会继续处理您的代码,直到 selected 文件,然后中断正常处理以处理 fileSelected 函数。
在您的代码中,这意味着 loadGame()
总是 运行 在您有机会 select 文件之前。解决方案是创建一个变量来检查文件是否已被 selected。
在代码的顶部创建变量:
boolean isFileSelected = false;
加载游戏前,检查文件是否selected:
if (gameLoaded == false && isFileSelected == true) {
loadGame();
gameLoaded = true;
}
select编辑文件时,更改变量:
void fileSelelected(File selection) {
if (selection == null) {
println("Nothing was selected, so nothing happens");
} else {
input = selection.getAbsolutePath();
isFileSelected = true;
}
}
如果还不是很清楚,下面的脚本可以帮助理解这个概念。一旦程序启动,它将继续打印 start
,直到您 select 编辑了一个文件。
String test = "start";
void setup() {
selectInput("Select a file to process:", "fileSelected");
}
void draw(){
println(test);
}
void fileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
println("User selected " + selection.getAbsolutePath());
test = "test";
}
}