# 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:** 1\
**Showing post:** 7

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

`:)`

---

_[View the full topic](https://discourse.processing.org/t/arduino-and-processing-gives-nullpointerexception/39399)._
