Hi, I’ve got a sketch that is supposed to load a file using loadStrings.
If for some reason that file is missing, then the sketch throws a null pointer exception and stops running.
I want to handle the case of the file being missing, and if it is, continue the sketch. I don’t want the whole app to crash because it can’t find a text file.
I haven’t found anything to handle a Null Pointer Exception.
Is there a way to trap for this error and continue if the exception is thrown?
I’m not really a programmer, so I might just be saying nonsense here. That said, the example linked is catching an IOException and it does hang the execution if there is no file. Probably because it’s throwing a NullPointer exception, not an IO one. If you, for instance, catches a generic Exception, the code does not hangs
BufferedReader reader;
String line;
void setup() {
// Open the file from the createWriter() example
reader = createReader("positions.txt");
}
void draw() {
try {
line = reader.readLine();
} catch (Exception e) { // < === !!! not IOException
e.printStackTrace();
line = null;
}
println(frameCount);
}
The NullPointerException could be catched by RuntimeException, but the right way would be to avoid it by checking the components if null.
BufferedReader reader;
String line;
void setup() {
// Open the file from the createWriter() example
reader = createReader("positions.txt");
If (reader == null) {
// Error loading file, do errorhandling
}
}
void draw() {
try {
// In case reader initialization succeed
line = reader.readLine();
If(line != null) {
// Do work with line
} else { // line null
// No more lines available, do errorhandling
}
} catch (Exception e) { // < === !!! not IOException
e.printStackTrace();
line = null;
} catch (RuntimeException r) {
// catches ie. NullPointerException
r.printStackTrace();
line = null;
}
println(frameCount);
}
Hope that helps …
Cheers
— mnse
PS: just typed on mobile, so excuse in case there are typos
Let us assume that you are trying to get the contents of a file called config.txt so we might have the statement like
String[] lines = loadStrings("config.txt");
If the file exists then the array lines will hold the contents of the file but what if the file does not exist? In that case the loadStrings method returns null so we can do something like
String[] lines = loadStrings("config.txt");
if(lines == null) {
// File does not exist so do whatever you need to do
}
else {
// File does exist so process the file contents i.e. the array lines
}
In Java mode Processing provides a wrapper to the Java language hiding many advanced features such as OO and exception handling from the novice programmer making it an easy way in to programming. Processing was originally designed to enable a computational graphics experience to non-programmers.
Of course novices becomes more competent at programming they discover the need to understand some advanced features of Java. :
The method loadStringsdoes not throw exceptions so there is never an exception to catch so any code in the catch block will never be executed. So the try/catch statement is redundant and should be removed.
In my code example we can split the variable declaration and definition like this
Given that Peter’s solution works perfectly, as well as others posted, I’m fairly certain you are 100% right. I must have been referencing the null file a little later.
I truly appreciate your wisdom and support - as well as everyone here who has responded to this thread.