使用SelectInput()

时间:2019-03-19 05:50:44

标签: nullpointerexception processing 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);
  }
}

1 个答案:

答案 0 :(得分:0)

通俗易懂,但是documentation建议文件选择器在单独的线程上运行。这意味着您的程序不会等到选择文件并完成selectInput-function之后。而是继续处理您的代码,直到选择了一个文件,然后正常处理被中断以处理fileSelected函数。

在您的代码中,这意味着loadGame()将始终运行,甚至没有机会选择文件。解决方案是创建一个变量,以检查是否已选择文件。

在代码顶部创建变量:

boolean isFileSelected = false;

在加载游戏之前,请检查是否已选择文件:

 if (gameLoaded == false && isFileSelected == true) {
    loadGame();
    gameLoaded = true;
  }

选择文件后,更改变量:

void fileSelelected(File selection) {
  if (selection == null) {
    println("Nothing was selected, so nothing happens");
  } else {
    input = selection.getAbsolutePath();
    isFileSelected = true;
  }
}

如果尚未完全清楚,以下脚本可以帮助您理解该概念。程序启动后,它将继续打印start,直到您选择了文件。

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";
  }
}