# Why do I get a NullPointerException

**URL:** https://discourse.processing.org/t/why-do-i-get-a-nullpointerexception/35711
**Category:** Electronics (Arduino, etc.)
**Created:** [March 13, 2022, 4:28pm UTC](https://discourse.processing.org/t/why-do-i-get-a-nullpointerexception/35711 "2022-03-13T16:28:37Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![LauraDob](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@LauraDob](https://discourse.processing.org/u/LauraDob)
#### Post date: [March 13, 2022, 4:28pm UTC](https://discourse.processing.org/t/why-do-i-get-a-nullpointerexception/35711/1 "2022-03-13T16:28:37Z")

</div>

When I run it, I have NullPointerException [0] “COM5”… How do I fix it? This is the code

import processing.serial.\*;  
Serial myPort; // Create object from Serial class  
int val = 0;  
int oldval = 0;  
int numFrames = 2;  
int frame = 0;  
PImage img;  
PImage[] images = new PImage[numFrames];

void setup()  
{  
size (1500, 1500);  
images[0] = loadImage(“fase1.jpg”);  
images[1] = loadImage(“fase2.jpg”);  
printArray(Serial.list());  
myPort = new Serial(this, “COM5”, 9600);  
}  
void draw()  
{  
if (myPort.available() \> 0) {  
val = myPort.read();  
}  
if (val != oldval && val == 2) {  
if (frame\<numFrames-1) {  
frame = (frame+1);  
} else {  
frame = 0;  
}  
}  
image(img, 0, 0);  
oldval = val;  
}

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 13, 2022, 4:36pm UTC](https://discourse.processing.org/t/why-do-i-get-a-nullpointerexception/35711/2 "2022-03-13T16:36:25Z")

</div>

Check kll’s post here: [Unable to receive serial data from Arduino - #3 by kll](https://discourse.processing.org/t/unable-to-receive-serial-data-from-arduino/14100/3)

---

<div class="post-metadata">

### Author: ![JSGauthier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jsgauthier/32/9910_2.png) [@JSGauthier](https://discourse.processing.org/u/JSGauthier)
#### Post date: [March 13, 2022, 4:37pm UTC](https://discourse.processing.org/t/why-do-i-get-a-nullpointerexception/35711/3 "2022-03-13T16:37:41Z")

</div>

Hi there,  
It is likely that you you are referencing a serial port that returns ‘null’. This means there is nothing listed as COM5.  
Perhaps if you comment out //myPort = new Serial(this. “COM5”, 9600);  
then read the print out in the console of the previous line -  
printArray(Serial.list());  
You may then  
-replace Com5 with a different value from the list and see if you code runs.  
or  
You can check if COM5 is listed with different syntax.

Hope this helps.  
Cheers,  
jsg

---

<div class="post-metadata">

### Author: ![LauraDob](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@LauraDob](https://discourse.processing.org/u/LauraDob)
#### Post date: [March 13, 2022, 8:28pm UTC](https://discourse.processing.org/t/why-do-i-get-a-nullpointerexception/35711/4 "2022-03-13T20:28:56Z")

</div>

Hi thank you for your help but i’m 100% certain that it is COM5. So that’s not it

---

<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: [March 13, 2022, 8:51pm UTC](https://discourse.processing.org/t/why-do-i-get-a-nullpointerexception/35711/5 "2022-03-13T20:51:11Z")

</div>

Hello @LauraDob,

Please format your code as a courtesy to this forum:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

You did not load an image and are trying to display it so it gives an error.

Here is an [MCVE](https://stackoverflow.com/help/minimal-reproducible-example):

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1ab694a94e751c49f834f192e2d99a2d8728a8bc.png)

Try displaying one of these images which you have loaded:

```auto
images[0] = loadImage("fase1.jpg");
images[1] = loadImage("fase2.jpg");

```

Reference:  
_[Why do I get a NullPointerException? - Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/8071/why-do-i-get-a-nullpointerexception.html)_

I do _ **not have** _ a `"COM 5"` and I get a `RuntimeException` for that:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f86c43b8db23f4535cdb9010e87349d093b6de17.png)

`:)`

---

<div class="post-metadata">

### Author: ![JSGauthier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jsgauthier/32/9910_2.png) [@JSGauthier](https://discourse.processing.org/u/JSGauthier)
#### Post date: [March 21, 2022, 2:32am UTC](https://discourse.processing.org/t/why-do-i-get-a-nullpointerexception/35711/6 "2022-03-21T02:32:52Z")

</div>

Hi @LauraDob,

I think I can see the error here. (Your insistance on COM5 being the right port was a good hint). Since you are certain that you want to reference “COM5” then you must refer to “COM5” via the address Serial.list()[index] (here the index is the value for “COM5” that is printed when you check the console following this line: printArray(Serial.list());

The console should tell you which Serial.list()[index to put].

(on my computer it prints: [0] “COM3”, it should list others like COM 5 also…)

So try changing your reference argument in Serial to something like this…

myPort = new Serial(this, Serial.list()[_place the index # here_], 9600);

Hope this helps.

Cordially,  
JS

PS: Check the example files for the Serial Library for more hints on syntax for the Serial library.

---

<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: [March 21, 2022, 10:52am UTC](https://discourse.processing.org/t/why-do-i-get-a-nullpointerexception/35711/7 "2022-03-21T10:52:01Z")

</div>

Hello,

The error in the code from @LauraDob in this case is because _img_ is _null_.

A minimal example:

```auto
import processing.serial.*;
Serial myPort; // Create object from Serial class

PImage img;
String url = "https://media.digikey.com/Renders/Stackpole%20Renders/RC12-56Ohm-5_tmb.jpg";

void setup()
  {
  size (100, 100);
  printArray(Serial.list());
  
  // Either one of these will work:
  myPort = new Serial(this, "COM9", 9600);
  //myPort = new Serial(this, Serial.list()[0], 9600);
  
  //img = loadImage(url); //It works with this uncommented
  }

void draw()
  {
  image(img, 18, 18);
  }

```

`"COM9"` is my Arduino COM port.

```auto
  // Either one of these will work and can be interchanged:
  myPort = new Serial(this, "COM9", 9600);
  //myPort = new Serial(this, Serial.list()[0], 9600);

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6e98cbd6e7131ee95b0df4adfb66527a792e6edc.png)

This is the error if you did not load an image:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/46cfd5d0ea7052bd0d28cd0320441a289403661a.png)

It works if you load an image.

`:)`

---

<div class="post-metadata">

### Author: ![JSGauthier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jsgauthier/32/9910_2.png) [@JSGauthier](https://discourse.processing.org/u/JSGauthier)
#### Post date: [March 21, 2022, 3:05pm UTC](https://discourse.processing.org/t/why-do-i-get-a-nullpointerexception/35711/8 "2022-03-21T15:05:10Z")

</div>

Thanks @glv,  
Your care and attention to detail is appreciated. I’ll task myself to apply that same care in my responses moving forward. Keep up the kind work!  
✌
