We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Null pointer exception when calling a .txt
Page Index Toggle Pages: 1
Null pointer exception when calling a .txt (Read 2432 times)
Null pointer exception when calling a .txt
Jun 14th, 2010, 7:03am
 
Hi,

The application I am creating needs a .txt file with important informations to run. When the user click on "read" the program searchs in the directory where the application is located the right document. The problem is, if there is no such file placed in a data folder next to the application, it freezes because of a "Null pointer exception". I need to tell the user in that case he has to create the folder and put the .txt file in it to run the application.

Is there a way to write a message to the user just before the Null pointer exception?
Re: Null pointer exception when calling a .txt
Reply #1 - Jun 14th, 2010, 7:30am
 
you can handle any exceptions that your code throws using try-catch

http://processing.org/reference/try.html

put the try block around the file open, and your message in the catch.
Re: Null pointer exception when calling a .txt
Reply #2 - Jun 14th, 2010, 7:47am
 
Okay I cheked the reference for Try statements and I can't find a way to identify the exception to write in the catch() parentesis.
Re: Null pointer exception when calling a .txt
Reply #3 - Jun 14th, 2010, 7:49am
 
Code:
try {
source = loadStrings(folder+"myfile.txt"); // Trouver l'emplacement enregistré plus haut et y ajouter le nom du fichier qui contient les données.
}
catch(NullPointerException e) {
textAlign(CENTER);
fill(0);
text("Impossible",width/2,height/2);
}


This is my code for now.
Re: Null pointer exception when calling a .txt
Reply #4 - Jun 14th, 2010, 8:03am
 
Okay my error, the NullPointerException wasn't in that particular line, it was in the next lines where I filled subStrings with parts of the the .txt file. Now it does works, thanks for your reference koogy.
Page Index Toggle Pages: 1