Resume Sketch after Null Pointer Exception?

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?

Thank you,

Mike

Hello @Thundercat,

This is what you are looking for:

`:)``

Thank you. I already was using try/catch.

It still generates a null pointer exception even in a try block. It really doesn’t like it when you try to load a file that is missing I guess.

It should throw the exception, but not hang the execution.

1 Like

I wish it did that, but it does hang the execution.

However, here is what I did. I decided to first check if the file exists, then use it:

File f = dataFile(dataPath("") +"/options.txt");
   String filePath = f.getPath();
   boolean blnOptionsExists = f.isFile();
   println(filePath, blnOptionsExists);
   
  if (blnOptionsExists) {//read the file
    try{
      optString = loadStrings(dataPath("") +"/options.txt");
     }
    catch(Exception e){
      e.printStackTrace();
      //showMsgBox(CRITICAL, "MusioTech", "Options file is missing!");     
     }
  }
  else {//create the file
    optString[0] = "Show Tooltips:";
    optString[1] = str(blnShowToolTips);
    optString[2] = "Extra Help Messages:";
    optString[3] = str(blnVerboseMessaging);
    saveStrings(dataPath("") +"/options.txt", optString);    
  }

This does work. But I’m bummed I can’t do it just with a try/catch…

Thank you @glv :slight_smile:

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);

}
1 Like

Yes, try/catch works in many cases. But I think you are right, since this is a Null Pointer Exception it doesn’t seem to work.

Thank you for posting your example.

Mike

1 Like

Hi @vkbr ,

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 :slight_smile:

2 Likes

@mnse thanks for the clarification and for your time : )

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. :smile: :

5 Likes

Thank you so much for explaining Peter.

Would there be any benefit to checking if the file exists as I did, or this just adds more steps?

Thanks again,

Mike

Think about it … loadStrings does both why add complications :grin:

1 Like

As per usual, your solution is better.

Eliminated lines of code, and got rid of a global var, which I think is always good.

Only thing is I can’t use “try/catch” with

try {
   String[] lines = loadStrings("config.txt");
}
catch (Exception e) {

}

Because following lines don’t see the lines var since it’s in a try/catch block.

However, even though it still generates an error in the console, it does not crash the sketch, so I like your solution better.

Thank you again Peter!

Mike

The method loadStrings does 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

// variable declaration
 String[] lines;

// variable definition (value assigned to variable)
lines = loadStrings("config.txt");
2 Likes

Thank you for that explanation.

I actually was getting Null Pointer Exceptions; I tried it many times. Clearly I missed something.

In any case, your solution works great, and I do like splitting the var dec up as it allows for the var to have a wider scope.

So once again for inadvertently teaching me processing. You’re a fabulous teacher!

Mike

1 Like

Hello @Thundercat,

This may be of interest:

And after I run it:

Perhaps you generated the exception at some point later in your code.

The Errors tab at the bottom provides the additional details about a potential Problem.

:)

2 Likes

This is great!

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.

Many thanks!!

All my best,

Mike

2 Likes