使程序的其余部分可以访问字符串的问题
Issues with making string accessable to rest of program
String[] input;
String output;
void setup() {
selectInput("Select a file to process:", "fileSelected")
println("########");
}
void draw() {
println(output);
}
void fileSelected(File selection) {
if(selection == null) {
println("Window was closed or the user hit 'cancel.'");
} else {
String filepath=selection.getAbsolutePath();
input=loadStrings(filepath);
println(input);
input.equals(output);
println(output);
}
}
我正在开发一个游戏项目,该项目需要将大型整数矩阵加载到二维数组中。我正在使用处理 3.4 并使用 selectInput()
方法,如参考中所示,并使用 loadStrings()
将文件内容加载到字符串中。
我无法将此代码发送到 运行,因为如果我尝试打印 'input' 的内容,我会得到讨厌的 'null pointer exception'。我不知道这是为什么,特别是因为该变量是一个全局变量。所以我声明使用 'output' 变量来解决空指针问题。我打印 input[]
和 output
的输出,以便我可以看到它们已经加载,我将 println(output);
放在 draw()
中以查看是否可以访问它。我得到的只是打印到我的控制台的“null”(不带引号)。
似乎 output
字符串总是空的。即使我确定它被声明为“全局级别”变量,该变量仍然为空。我需要变量可以在 public/global 级别访问,以便游戏代码的其余部分可以将字符串转换为矩阵(我没有在此处包含它,因为它不重要)。
如何加载此字符串以便我的其余代码可以使用它?
输出字符串始终为空,因为您没有将输入复制到其中,equals 方法不能那样工作。我修复了你的代码,它工作正常
String[] input;
String output;
void setup() {
selectInput("Select a file to process:", "fileSelected");
println("########");
}
void draw() {
if(output!=null)
println(output);
}
void fileSelected(File selection) {
if(selection == null)
{
println("Window was closed or the user hit 'cancel.'");
}
else {
String filepath=selection.getAbsolutePath();
input=loadStrings(filepath);
for(int i=0;i<input.length;i++)
output+=input[i]+"\n";
}
}
String[] input;
String output;
void setup() {
selectInput("Select a file to process:", "fileSelected")
println("########");
}
void draw() {
println(output);
}
void fileSelected(File selection) {
if(selection == null) {
println("Window was closed or the user hit 'cancel.'");
} else {
String filepath=selection.getAbsolutePath();
input=loadStrings(filepath);
println(input);
input.equals(output);
println(output);
}
}
我正在开发一个游戏项目,该项目需要将大型整数矩阵加载到二维数组中。我正在使用处理 3.4 并使用 selectInput()
方法,如参考中所示,并使用 loadStrings()
将文件内容加载到字符串中。
我无法将此代码发送到 运行,因为如果我尝试打印 'input' 的内容,我会得到讨厌的 'null pointer exception'。我不知道这是为什么,特别是因为该变量是一个全局变量。所以我声明使用 'output' 变量来解决空指针问题。我打印 input[]
和 output
的输出,以便我可以看到它们已经加载,我将 println(output);
放在 draw()
中以查看是否可以访问它。我得到的只是打印到我的控制台的“null”(不带引号)。
似乎 output
字符串总是空的。即使我确定它被声明为“全局级别”变量,该变量仍然为空。我需要变量可以在 public/global 级别访问,以便游戏代码的其余部分可以将字符串转换为矩阵(我没有在此处包含它,因为它不重要)。
如何加载此字符串以便我的其余代码可以使用它?
输出字符串始终为空,因为您没有将输入复制到其中,equals 方法不能那样工作。我修复了你的代码,它工作正常
String[] input;
String output;
void setup() {
selectInput("Select a file to process:", "fileSelected");
println("########");
}
void draw() {
if(output!=null)
println(output);
}
void fileSelected(File selection) {
if(selection == null)
{
println("Window was closed or the user hit 'cancel.'");
}
else {
String filepath=selection.getAbsolutePath();
input=loadStrings(filepath);
for(int i=0;i<input.length;i++)
output+=input[i]+"\n";
}
}