Arduino and Processing gives NullPointerException

Hi all! Just started with making a game in processing, connected to Arduino where I am using a joystick. Super exciting, however I am a bit stuck because I keep getting the following error message:

NullPointerException
Could not run the sketch (Target VM failed to initialize).

I have tested with easier examples but I don’t understand what this error exactly means. Does it have something to do with the Array?

Here is the code:

/*
DATE: 23_10_2022
 CREATIVE CHALLENGE 2: EGBERT THE FISH GAME_version_3
 AUTHOR: TAISSIA VISSER
 REFERENCES:
 https://www.youtube.com/watch?v=jUmjbFsA2zw&ab_channel=Crazy-Logic
 */

// Importing the serial library.
import processing.serial.*;

// Declare serial communication port
Serial myPort;
String inString;
String[] data;    // Array of data received from the serial port

// Declare game characters
PImage Campus;
PImage Egbert;
PImage Bottle;
PImage Corn;
PImage Bread;

void setup() {
  size (1920, 1081);
  String myPortName = Serial.list()[1];
  myPort = new Serial(this, myPortName, 9600);
  myPort.bufferUntil('\n');

  //Campus = loadImage("Egbertbackground.jpg");
  //Egbert = loadImage("Egbert.png");
  //Bottle = loadImage("plastic.png");
  //Corn = loadImage("corn (1).png");
  //Bread = loadImage("bread.png");
}

void draw() {
  int xPos = int(data[0])/2;
  int yPos = int(data[1])/2;
  int Pressed = int(data[2]);
  //background(Campus);
  image(Egbert, xPos, yPos);
  //image(Bottle, 100, 100);
  //image(Corn, 300, 300);
  //image(Bread, 400, 200);
}

// will get called every time data comes in
// storing the data string here
void serialEvent(Serial myPort) {
  String inString = myPort.readStringUntil('\n');
  if (inString != null)
    // removing the whitespace
    inString = trim(inString);
  // Splitting the string at the commas and
  // seperating the variables out into an array
  data = split(inString, ",");
}

I can also provide you with the arduino code/ files I have created as a background etc. but I am not sure if that is needed.

Hope someone knows what I have done wrong!

PS: I am making a game about a fish named Egbert who lives in the pond at my University campus :stuck_out_tongue:

Thanks :slight_smile:

1 Like

This is a bug

Please restart processing or the computer

1 Like

Use 0 Instead maybe

Chrisir

Hello @Ties2901,

For the NullPointerException:

https://forum.processing.org/two/discussion/8071/why-do-i-get-a-nullpointerexception

if data[] has not yet been initialized it is a null.
You are trying to read this in draw() and it gives a NullPointerException if there is no data received yet in data[].

Try this in draw():

  if(data != null)
  {
  // Your code...
  }

Take a look at how the code checks for null in the serialEvent().

There are also other ways to check and flag valid data… welcome to serial programming!

Which version of Processing are you using?

If I get a sketch that is locked up I will sometimes wait it out…

The other option is to use Windows Task Manager and for “OpenJDK Platform binary” you will “End task”:

image

Make sure you routinely save files so you do not lose your work.

This is good to add to setup() to check available serial ports:
https://processing.org/reference/libraries/serial/Serial_list_.html

This is a common error if you select a serial port this is not available:

Update:

This is what your code is doing if inString is null and generates a NullPointerException:

String [] data;

void setup()
  {
  //String inString = "0.1,2.1,3.3" + "\r\n";
  String inString = null;
  if (inString != null)
    inString = trim(inString);
    
  // It may still be null here... 
  data = split(inString, ",");
  
  printArray(data); 
  }

This will catch the null:

String [] data;

void setup()
  {
  //String inString = "0.1,2.1,3.3" + "\r\n"; // Try with this... 
  String inString = null;  // Or with this 
  if (inString != null)
    {  
    inString = trim(inString);
    data = split(inString, ",");
    printArray(data); 
    }
  }

You may also want to add a check to see of the array size is correct after receiving data.

:)

2 Likes

I have reinstalled processing and also changed the port number inside the brackets but the same error keeps occurring unfortunately. I followed the following tutorial:

Hi glv,

Thanks for your reply and explanation.
I understand that if data is not initialized it is null so I have to clarify what processing needs to do if data is not null, but receiving. Placing this in my code has helped with the nullpointerexception so that is good! I can run the sketch now, but nothing is happening.
When I place the background outside of the IF statement I get the nullpointer exception again… That is strange because I also want to display the background when I am not recieving any data and so when data is null. I am confused because when I look in the Arduino IDE that I am using, the serial monitor is clearly receiving data of the joystick when I move it.

I have also checked which Serial Port i was supposed to be using! Thanks for the tip, I was in the wrong port.

Regarding checking for null in the serialEvent() function i have used the general code that my University has given.

I also have now the problem that when I am typing in Processing, my text ‘overlaps’ and writes over + deletes the text that is allready there… any shortcuts to stop this from happening?

Thanks

/*
DATE: 23_10_2022
 CREATIVE CHALLENGE 2: EGBERT THE FISH GAME_version_3
 AUTHOR: TAISSIA VISSER
 REFERENCES:
 https://www.youtube.com/watch?v=jUmjbFsA2zw&ab_channel=Crazy-Logic
 */

// Importing the serial library.
import processing.serial.*;

// Declare serial communication port
Serial myPort;
String inString;
String[] data;    // Array of data received from the serial port

// Declare game characters
PImage Campus;
PImage Egbert;
PImage Bottle;
PImage Corn;
PImage Bread;

void setup() {
  size (1920, 1081);
  String myPortName = Serial.list()[2];
  myPort = new Serial(this, myPortName, 9600);
  myPort.bufferUntil('\n');
  //intArray(Serial.list());

  //Campus = loadImage("Egbertbackground.jpg");
  //Egbert = loadImage("Egbert.png");
  //Bottle = loadImage("plastic.png");
  //Corn = loadImage("corn (1).png");
  //Bread = loadImage("bread.png");
}

void draw() {
  background(Campus);
  if (data != null)
  {
    int xPos = int(data[0])/2;
    int yPos = int(data[1])/2;
    int Pressed = int(data[2]);
    image(Egbert, xPos, yPos);
  }
  //image(Bottle, 100, 100);
  //image(Corn, 300, 300);
  //image(Bread, 400, 200);
}

// will get called every time data comes in
// storing the data string here
void serialEvent(Serial myPort) {
  String inString = myPort.readStringUntil('\n');
  if (inString != null)
    // removing the whitespace
    inString = trim(inString);
  // Splitting the string at the commas and
  // seperating the variables out into an array
  data = split(inString, ",");
}

That is because you have commented this:

  //Campus = loadImage("Egbertbackground.jpg");

Try this example and tell me what you are seeing:

Arduino Code < Click to open!
void setup() 
  {
  Serial.begin(9600);
  delay(500);
  }

int c;

void loop() 
  {
  float angle = c*TWO_PI/360;  
  int x = int(200*cos(angle));
  int y = int(200*sin(angle));
  Serial.print(x);
  Serial.print(',');
  Serial.print(y);
  Serial.print(',');
  Serial.println(c);
  delay(10);        // delay in between reads for stability
  c++;
  if(c>359) c = 0;
  }
Processing Code < Click to open!
/*
DATE: 23_10_2022
 CREATIVE CHALLENGE 2: EGBERT THE FISH GAME_version_3
 AUTHOR: TAISSIA VISSER
 REFERENCES:
 https://www.youtube.com/watch?v=jUmjbFsA2zw&ab_channel=Crazy-Logic
 */

// Modified by glv for testing. 2022/10/24

// Importing the serial library.
import processing.serial.*;

// Declare serial communication port
Serial myPort;
//String inString;
String[] data = {"0", "0", "0"};    // Array of data received from the serial port

void setup() 
  {
  size (400, 400);
  String myPortName = Serial.list()[5];
  myPort = new Serial(this, myPortName, 9600);
  myPort.bufferUntil('\n');
  //PrintArray(Serial.list());
  }

void draw() {
  //background(Campus);
  background(0);
  int xPos = int(data[0])/2;
  int yPos = int(data[1])/2;
  int Pressed = int(data[2]);
  //image(Egbert, xPos, yPos);
  circle(xPos+200, yPos+200, 30);
}

// will get called every time data comes in
// storing the data string here
void serialEvent(Serial myPort) {
  String inString = myPort.readStringUntil('\n');
  if (inString != null)
    {
    // removing the whitespace
    inString = trim(inString);
  // Splitting the string at the commas and
  // seperating the variables out into an array
    data = split(inString, ",");
    }
}

I initialized data to avoid the null until you received data:
String[] data = {"0", "0", "0"};

Focus on receiving data for now and refine and add to the code later.

I am just helping you along…

What operating system and which version of Processing?

Related:
https://discourse.processing.org/t/text-indicator-not-in-the-right-place/32073/3

The Processing Preferences has something related HiDPI to scaling:

I am using W10 and Processing 4.01.

:)

1 Like

I figured out that I did something wrong in my Arduino IDE code. The last sentence that I wrote was print.serial. But it had to be println.serial. Apperantly this was needed because I was reading the data in processing until there would be another line. (/u)