# Arduino and Processing gives NullPointerException

**URL:** <https://discourse.processing.org/t/arduino-and-processing-gives-nullpointerexception/39399>\
**Category:** Electronics (Arduino, etc.)\
**Tags:** homework\
**Created:** [October 23, 2022, 3:08pm UTC](https://discourse.processing.org/t/arduino-and-processing-gives-nullpointerexception/39399 "2022-10-23T15:08:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ties2901](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@Ties2901](https://discourse.processing.org/u/Ties2901)\
**Post date:** [October 23, 2022, 3:08pm UTC](https://discourse.processing.org/t/arduino-and-processing-gives-nullpointerexception/39399/1 "2022-10-23T15:08:18Z")

</div>

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:

```auto
/*
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 😛

Thanks 🙂

---

<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:** [October 23, 2022, 3:21pm UTC](https://discourse.processing.org/t/arduino-and-processing-gives-nullpointerexception/39399/2 "2022-10-23T15:21:50Z")

</div>

This is a bug

Please restart processing or the computer

---

<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:** [October 23, 2022, 3:22pm UTC](https://discourse.processing.org/t/arduino-and-processing-gives-nullpointerexception/39399/3 "2022-10-23T15:22:49Z")

</div>

> [@Ties2901](#):
>
> `String myPortName = Serial.list()[0];`

Use 0 Instead maybe

Chrisir

---

<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:** [October 23, 2022, 3:44pm UTC](https://discourse.processing.org/t/arduino-and-processing-gives-nullpointerexception/39399/4 "2022-10-23T15:44:23Z")

</div>

Hello @Ties2901,

For the _NullPointerException_:

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

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/a/eaa0cde602598290824c7cabb50ca5b675782dbd.png)

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()`:

```auto
  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…

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/d/a/da5d69a03af40463bb9561dc2c03af0bf46c577d.png)

The other option is to use _Windows Task Manager_ and for “_OpenJDK Platform binary_” you will “_End task_”:

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/a/9aae1f8e92886ed1e3ae2b8572ab8429448699d1.png)

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](https://processing.org/reference/libraries/serial/Serial_list_.html)

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

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/7/b/7bf44fc7055cdf714bd9608d517b17f91b9ba51c.png)

Update:

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

```auto
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:

```auto
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.

`:)`

---

<div class="post-metadata">

**Author:** ![Ties2901](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@Ties2901](https://discourse.processing.org/u/Ties2901)\
**Post date:** [October 24, 2022, 10:04am UTC](https://discourse.processing.org/t/arduino-and-processing-gives-nullpointerexception/39399/5 "2022-10-24T10:04:10Z")

</div>

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:

[![](https://img.youtube.com/vi/jUmjbFsA2zw/maxresdefault.jpg "#13 cheap Joystick via serial from Arduino to Processing on Ubuntu") ](https://www.youtube.com/watch?v=jUmjbFsA2zw)

---

<div class="post-metadata">

**Author:** ![Ties2901](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@Ties2901](https://discourse.processing.org/u/Ties2901)\
**Post date:** [October 24, 2022, 10:57am UTC](https://discourse.processing.org/t/arduino-and-processing-gives-nullpointerexception/39399/6 "2022-10-24T10:57:41Z")

</div>

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

```auto
/*
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, ",");
}

```

---

<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:** [October 24, 2022, 10:52pm UTC](https://discourse.processing.org/t/arduino-and-processing-gives-nullpointerexception/39399/7 "2022-10-24T22:52:34Z")

</div>

> [@Ties2901](#):
>
> When I place the background outside of the IF statement I get the nullpointer exception again…

That is because you have commented this:

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

```

Try this example and tell me what you are seeing:

> **Arduino Code \< Click to open!**
>
> ```auto
> 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!**
>
> ```auto
> /*
> 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…

> [@Ties2901](#):
>
> 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?

What operating system and which version of Processing?

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

The Processing _Preferences_ has something related _HiDPI_ to scaling:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/7/2/72c677315a29af806868ab715c1a738477b7a787.png)

I am using W10 and Processing 4.01.

`:)`

---

<div class="post-metadata">

**Author:** ![Ties2901](https://avatars.discourse-cdn.com/v4/letter/t/bcef8e/32.png) [@Ties2901](https://discourse.processing.org/u/Ties2901)\
**Post date:** [October 28, 2022, 3:05pm UTC](https://discourse.processing.org/t/arduino-and-processing-gives-nullpointerexception/39399/8 "2022-10-28T15:05:40Z")

</div>

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)
