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];
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.
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.
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);
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!