Why do I get a NullPointerException

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.

:)

1 Like