# Resume Sketch after Null Pointer Exception?

**URL:** <https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572>\
**Category:** Processing\
**Created:** [June 10, 2024, 5:26pm UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572 "2024-06-10T17:26:00Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thundercat](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Thundercat](https://discourse.processing.org/u/Thundercat)\
**Post date:** [June 10, 2024, 5:26pm UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/1 "2024-06-10T17:26:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 10, 2024, 5:36pm UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/2 "2024-06-10T17:36:10Z")

</div>

Hello @Thundercat,

This is what you are looking for:

> **[try / Reference](https://processing.org/reference/try.html)**
>
> The try keyword is used with catch to handle exceptions. Sun's Java documentation defines an exception as "an event, which occurs during the execution of a program, that disrupts the nor…

`:)``

---

<div class="post-metadata">

**Author:** ![Thundercat](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Thundercat](https://discourse.processing.org/u/Thundercat)\
**Post date:** [June 10, 2024, 6:15pm UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/3 "2024-06-10T18:15:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)\
**Post date:** [June 10, 2024, 6:21pm UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/4 "2024-06-10T18:21:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Thundercat](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Thundercat](https://discourse.processing.org/u/Thundercat)\
**Post date:** [June 10, 2024, 6:22pm UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/5 "2024-06-10T18:22:48Z")

</div>

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:

```auto
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 🙂

---

<div class="post-metadata">

**Author:** ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)\
**Post date:** [June 10, 2024, 6:32pm UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/6 "2024-06-10T18:32:10Z")

</div>

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

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

}

```

---

<div class="post-metadata">

**Author:** ![Thundercat](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Thundercat](https://discourse.processing.org/u/Thundercat)\
**Post date:** [June 10, 2024, 6:46pm UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/7 "2024-06-10T18:46:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [June 10, 2024, 7:41pm UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/8 "2024-06-10T19:41:47Z")

</div>

Hi @vkbr ,

The NullPointerException could be catched by RuntimeException, but the right way would be to avoid it by checking the components if null.

```auto
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 🙂

---

<div class="post-metadata">

**Author:** ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)\
**Post date:** [June 10, 2024, 8:09pm UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/9 "2024-06-10T20:09:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [June 10, 2024, 9:29pm UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/10 "2024-06-10T21:29:31Z")

</div>

> [@Thundercat](#):
>
> I’ve got a sketch that is supposed to load a file using loadStrings.

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

```auto
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. 😄 :

---

<div class="post-metadata">

**Author:** ![Thundercat](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Thundercat](https://discourse.processing.org/u/Thundercat)\
**Post date:** [June 11, 2024, 1:20am UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/11 "2024-06-11T01:20:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [June 11, 2024, 8:06am UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/12 "2024-06-11T08:06:54Z")

</div>

> [@Thundercat](#):
>
> Would there be any benefit to checking if the file exists

Think about it … loadStrings does both why add complications 😁

---

<div class="post-metadata">

**Author:** ![Thundercat](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Thundercat](https://discourse.processing.org/u/Thundercat)\
**Post date:** [June 11, 2024, 12:18pm UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/13 "2024-06-11T12:18:57Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [June 11, 2024, 1:08pm UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/14 "2024-06-11T13:08:09Z")

</div>

> [@Thundercat](#):
>
> Only thing is I can’t use “try/catch" with
> 
> ```auto
> try {
> String[] lines = loadStrings("config.txt");
> }
> catch (Exception e) {
> 
> }
> 
> ```

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

```auto
// variable declaration
 String[] lines;

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

```

---

<div class="post-metadata">

**Author:** ![Thundercat](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Thundercat](https://discourse.processing.org/u/Thundercat)\
**Post date:** [June 11, 2024, 1:59pm UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/15 "2024-06-11T13:59:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 11, 2024, 11:24pm UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/16 "2024-06-11T23:24:16Z")

</div>

Hello @Thundercat,

> [@Thundercat](#):
>
> I actually was getting Null Pointer Exceptions; I tried it many times. Clearly I missed something.

This may be of interest:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/9/892f7659bd83aef755889c7004b770f7908e1e81.png)

And after I run it:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/c/8/c8f0c8c8d0a27864350339150b47ce3b42a7d9b6.png)

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_.

`:)`

---

<div class="post-metadata">

**Author:** ![Thundercat](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Thundercat](https://discourse.processing.org/u/Thundercat)\
**Post date:** [June 12, 2024, 3:57am UTC](https://discourse.processing.org/t/resume-sketch-after-null-pointer-exception/44572/17 "2024-06-12T03:57:53Z")

</div>

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
