Why do I get a NullPointerException

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

Check kll’s post here: Unable to receive serial data from Arduino - #3 by kll

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

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

Hello @LauraDob,

Please format your code as a courtesy to this forum:
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:

Try displaying one of these images which you have loaded:

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

Reference:
Why do I get a NullPointerException? - Processing 2.x and 3.x Forum

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

:)

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.

Hello,

The error in the code from @LauraDob in this case is because img is null.

A minimal example:

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.

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

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

It works if you load an image.

:)

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!
:v: