# Need help finding whats causing a NullPointerException

**URL:** <https://discourse.processing.org/t/need-help-finding-whats-causing-a-nullpointerexception/13694>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [August 29, 2019, 6:30pm UTC](https://discourse.processing.org/t/need-help-finding-whats-causing-a-nullpointerexception/13694 "2019-08-29T18:30:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kasdriu](https://avatars.discourse-cdn.com/v4/letter/k/54ee81/32.png) [@Kasdriu](https://discourse.processing.org/u/Kasdriu)\
**Post date:** [August 29, 2019, 6:30pm UTC](https://discourse.processing.org/t/need-help-finding-whats-causing-a-nullpointerexception/13694/1 "2019-08-29T18:30:34Z")

</div>

Hi, Ive been trying to send a string value from Arduino to Processing but I keep getting a NullPointerException when it gets to this line

```auto
if (val.equals("hola"))
  {
    //file.play();
    println("Funciona");
  }

```

I know the value gets store in the val variable but for some reason when it tries to compare the val value with whats inside the .equals it throws the NullPointerException. Ive tried looking to a bunch of tutorial with no success. Im sure its a pretty simple mistake but i dont know enough about processing to really get it. Any help would be appreciated. I was following the first part of this tutorial [https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all](https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all) and tried to mix it with the equals() function.

This is my Arduino Code:

```auto
int fotoCelda1;

boolean fotoceldaDown = 0;

void setup()
{
  
  Serial.begin(9600);
  Serial.println("inicial");
}

void loop()
{
  fotoCelda1 = analogRead(0);     
  
  if (fotoCelda1 < 800)
  {
    fotoceldaDown = true;
  } 
  else 
  {
    fotoceldaDown = false;
  }

  if (fotoceldaDown)
  {
    Serial.println("hola");
    delay(300);
    fotoceldaDown = false;
  }else
  {
    Serial.println("chao");
    delay(300);
  }

}

```

This is my Processing code:

```auto
import processing.serial.*;

Serial myPort; // Create object from Serial class
String val=" "; // Data received from the serial port

import processing.sound.*;
SoundFile file;

void setup() {
  size(640, 360);
  background(255);
  
  String portName = Serial.list()[0]; 
  myPort = new Serial(this, portName, 9600);  
  myPort.bufferUntil('\n');

  
  file = new SoundFile(this, "Cucarachero.mp3");
 
}

void draw() {

  if ( myPort.available() > 0) 
  {  
    val = myPort.readStringUntil('\n');
    
  } 
  println(val);

  if (val.equals("hola"))
  {
    //file.play();
    println("Funciona");
  }
  else
  {
   println("no entre en panico"); 
  }
}

```

Thank you again!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [August 29, 2019, 10:23pm UTC](https://discourse.processing.org/t/need-help-finding-whats-causing-a-nullpointerexception/13694/2 "2019-08-29T22:23:24Z")

</div>

and what does the console print show?  
even at the linked tutorial you see in a screenshot that with that processing code  
he got sometimes “null” ( and you can not do a .equal with that )

a better serial catch would use:  
[https://processing.org/reference/libraries/serial/serialEvent\_.html](https://processing.org/reference/libraries/serial/serialEvent_.html)  
and in that  
[https://processing.org/reference/trim\_.html](https://processing.org/reference/trim_.html)  
add must do a  
check on NOT NULL  
[https://processing.org/reference/null.html](https://processing.org/reference/null.html)  
only after that can store the INSTRING to a user variable

TESTED with your original arduino code:

```auto
import processing.serial.*;

Serial myPort; 
String data, val="NaN";
int status=-1;

void setup_serial() { // USB arduino..
  printArray(Serial.list());
  String portName = Serial.list()[0]; 
  myPort = new Serial(this, portName, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  println("try connect to "+portName);
}

void serialEvent(Serial p) { // handle serial data
  data = trim(p.readStringUntil('\n'));
  if (data != null) {
    val = data;
    if ( val.equals("inicial") ) status = 0;
    if ( val.equals("hola") ) status = 1;
    if ( val.equals("chao") ) status = 2;
    println("val "+val+" status "+status); // print every GOOD line and if it is understood 
  }
}

void setup() {
  size(640, 360);
  setup_serial();
}

void draw(){}

```

and with this cleaned up version

```auto
int fotoCelda1; // 0 .. 1023
int fotoCeldaPin = 0; // A0
boolean fotoceldaDown = 0; // not needed

void setup() {
  Serial.begin(9600);
  Serial.println("inicial");
}

void loop() {
  fotoCelda1 = analogRead(fotoCeldaPin);
  if (fotoCelda1 < 800) {
    fotoceldaDown = true;
    Serial.println("hola");
  } else {
    fotoceldaDown = false;
    Serial.println("chao");
  }
  delay(300); // about 3 lines per sec
}

```
