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);
}
}
I am working on a game project that needs to have a large matrix of integers loaded into a 2D array. I am using processing 3.4 and was using the selectInput()
method as shown in the reference and loading the contents of a file into a string using loadStrings()
like so.
I couldn't get this code to run because if I try to print the contents of 'input' I get the hated 'null pointer exception'. I don't know why this is, especially because the variable is a global variable. So I stated to use the 'output' variable to get around the null pointer issue. I print the output of input[]
and output
so that I can see that they have loaded, and I put the println(output);
in draw()
to see if I can access it. All I get is “null” (without quotes) printed to my console.
It appears that the output
string is always empty. Even when I mades sure that is was declared as a “global level” variable, the variable is still null. I need the variable to be accessible on a public/global level so that the rest of the game code can convert the string into a matrix ( which I didn’t include here because it isn't important).
How can I load this string so that the rest of my code can use it?
The output string is always empty because you are not copying the input into it, the method equals doesn't work like that. I fixed your code and it works fine
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";
}
}