Need help finding whats causing a NullPointerException

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

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 and tried to mix it with the equals() function.

This is my Arduino Code:

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:

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!

1 Like

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
and in that
https://processing.org/reference/trim_.html
add must do a
check on NOT NULL
https://processing.org/reference/null.html
only after that can store the INSTRING to a user variable

TESTED with your original arduino code:

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

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
}

2 Likes