Make a Simple GUI and Displaying Serial Arduino data got NullPointerException

Hello I want to make an Arduino serial data display using processing from several sensors, but for experiments only I use 3 potentiometers. Here’s the Arduino code.

#define pinPot1 0
#define pinPot2 3
#define pinPot3 5


float pot1;
float pot2;
float pot3;



float rpm1;
float rpm2;
float Tinlet;
float Texhaust;
float Tling;
float RH;
float Load;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}


void loop() {

  pot1 = analogRead (pinPot1);
  pot2 = analogRead (pinPot2);
  pot3 = analogRead (pinPot3);

  rpm1  = ((pot1 * 3) + 1000);
  rpm2  = ((pot1 * 6) + 1000);
  Tinlet = ((pot2 / 100) + 20);
  Texhaust = ((pot2 / 100) + 40);
  Tling = ((pot3 / 100) + 15);
  RH = ((pot3 / 100) + 50);
  Load = (((pot3 / 100) * 0.8) + 10);


  Serial.print(rpm1);
  Serial.print(",");
  Serial.print(rpm2);
  Serial.print(",");
  Serial.print(Load);
  Serial.print(",");
  Serial.print(Tinlet);
  Serial.print(",");
  Serial.print(Texhaust);
  Serial.print(",");
  Serial.print(Tling);
  Serial.print(",");
  Serial.print(RH);

  Serial.println();

  delay (100);


}

But when I try to display the data with the code in processing I encounter a Nullpointerexception error. I’ve tried to learn what a Nullpointerexception is but for its use in my code, I don’t understand. Any suggestions for me?
Here’s the code processing that I made

//Simple Dyno Logger

import processing.serial.*;
import controlP5.*;


Serial serial;
ControlP5 cp5;

int speed = 9600;
String portName;
String filename = "contoh";
Table table;

//Variabel Date
int d = day();
int m = month();
int y = year();

//Variable Time
int h = hour();
int min = minute();
int s = second();

//Variable value from serial
float rpm1;
float rpm2;
float torsi;
float daya;
float load;
float tinlet;
float texhaust;
float temp;
float hum;

//variable panjang lengan dyno
float l = 0.152;


void setup() {
  size(600, 400);
  cp5 = new ControlP5(this);

  cp5.addButton("refresh")
    .setPosition(110, 0)
    .setSize(50, 30);
  cp5.addButton("openPort")
    .setPosition(170, 0)
    .setSize(50, 30);
  cp5.addButton("closePort")
    .setPosition(230, 0)
    .setSize(60, 30);
  cp5.addButton("ledOn")
    .setPosition(300, 0)
    .setSize(50, 30);
  cp5.addButton("ledOff")
    .setPosition(360, 0)
    .setSize(50, 30);
  cp5.addScrollableList("comlist")
    .setPosition(0, 0)
    .setSize(100, 150)
    .setBarHeight(30)
    .setItemHeight(30).close();

  table = new Table();

  table.addColumn("Date", Table.STRING);
  table.addColumn("Nomor", Table.INT);
  table.addColumn("Time", Table.STRING);
  table.addColumn("RPM Roller", Table.FLOAT);
  table.addColumn("RPM Engine", Table.FLOAT);
  table.addColumn("Torsi (N.m)", Table.FLOAT);
  table.addColumn("Daya (HP)", Table.FLOAT);
  table.addColumn("Load (Kg)", Table.FLOAT);
  table.addColumn("Tinlet (°C)", Table.FLOAT);
  table.addColumn("Texhaust (°C)", Table.FLOAT);
  table.addColumn("Temp (°C)", Table.FLOAT);
  table.addColumn("Humidity (%)", Table.FLOAT);

  TableRow newRow = table.addRow();

  newRow.setString("Date", str(d) + "/" + str (m) + "/" + str(y));
}

void refresh() {
  String list[] = Serial.list();
  cp5.get(ScrollableList.class, "comlist").addItems(list);
}
void comlist(int n) {
  portName = Serial.list()[n];
}
void openPort() {
  serial = new Serial(this, portName, speed);
}
void closePort() {
  serial.stop();
}
void ledOn() {
  serial.write('n');
}
void ledOff() {
  serial.write('f');
}

void draw() {
  background(120);

  if (serial != null) {
    if (serial.available() > 0) {
      String val = serial.readStringUntil('\n');
      String [] data = split(val, ',');
      rpm1 = float(data[0]);
      rpm2 = float(data[1]);
      load = float(data[2]);
      tinlet = float(data[3]);
      texhaust = float(data[4]);
      temp = float(data[5]);
      hum = float(data[6]);

      //Data Torsi dan Daya
      torsi = (rpm2 * l)/100;
      daya  = (rpm2 * torsi)/100;
    }
    TableRow newRow = table.addRow();

    newRow.setInt("Nomor", table.getRowCount());
    newRow.setString("Time", str(h) + ":" + str(min) + ":" + str(s));
    newRow.setFloat("RPM Roller", rpm1);
    newRow.setFloat("RPM Engine", rpm2);
    newRow.setFloat("Torsi (N.m)", torsi);
    newRow.setFloat("Daya (HP)", daya);
    newRow.setFloat("Load (Kg)", load);
    newRow.setFloat("Tinlet (°C)", tinlet);
    newRow.setFloat("Texhaust (°C)", texhaust);
    newRow.setFloat("Temp (°C)", temp);
    newRow.setFloat("Humidity (%)", hum);

    // saveTable(table, "data/new2.csv");
  }

  //make a rect as a value box
  stroke(255);
  fill(0, 50, 100);
  rect(10, 80, 40, 40, 4);
  rect(60, 160, 40, 40, 4);
  rect(110, 80, 40, 40, 4);
  rect(160, 160, 40, 40, 4);
  rect(210, 80, 40, 40, 4);
  rect(260, 160, 40, 40, 4);
  rect(310, 80, 40, 40, 4);
  rect(360, 160, 40, 40, 4);
  rect(410, 80, 40, 40, 4);

  //make a lable for each value
  PFont f = createFont ("Georgia", 15);
  textFont (f);
  fill(255);
  text("Rpm Roll", 0, 70);
  text("Rpm Engine", 100, 70);
  text("Torsi (N.m)", 200, 70);
  text("Daya (HP)", 300, 70);
  text("Load (Kg)", 400, 70);
  text("Tinlet (°C)", 50, 150);
  text("Texhaust (°C)", 150, 150);
  text("Temp. (°C)", 250, 150);
  text("Humidity (%)", 350, 150);

  //write value on rect
  int x = 105;
  int y = 185;

  PFont t = createFont ("arial", 15);
  textFont (t);
  fill(255);
  text(rpm1, 15, x);
  text(rpm2, 115, x);
  text(torsi, 220, x);
  text(daya, 320, x);
  text(load, 420, x);
  text(tinlet, 70, y);
  text(texhaust, 170, y);
  text(temp, 270, y);
  text(hum, 370, y);

  //Write SIMPLE DYNOTEST
  stroke(255);
  textFont(f);
  textSize(60);
  fill(255, 255, 255);
  text("SIMPLE DYNOTEST", 20, 300);
}

I’ve tried with this link but still got Nullpointerexception.

Hi @anwar05. You haven’t said where the error is. Almost certainly it’s where you are processing the received text. Print out what you receive before you do the ‘split’, and print data.length. Then you’ll see that sometimes(?) the data is not what you expect. (Why not?) Where you are reading float(data[<>]) you could protect that with 'if (data.length >= 7). In general two approaches, fix the data so it is what you expect, make the code tolerate wrong data.

Hello @RichardDL, when I run my program an error occurs on line 116, where the error is null pointer exception.
how do i print the data length?, and after i print it, i have to make sure the data is 7 using If (data.length >=7) ?

I tried to print out the data with a simpler program, and the result is like this. The results shown by the processing console are the same as those displayed on the Arduino serial monitor. My program is attached below the photo
image

//Test Print data
import processing.serial.*;

Serial port;
String val;

void setup() {
  size(600, 360);
  background(255);

  port = new Serial(this, "COM8", 9600);
}

void draw() {

  textSize(15);
  fill(0, 255, 0);
  text("TEST PRINT DATA", width/2, height/2);
  
  if (port.available() > 0) 
  {  
    val = port.readStringUntil('\n');         // read it and store it in val
 
  } 
println(val); //print it out in the console
}


Hello,

Arduino test code:

void setup() 
  {
  Serial.begin(9600);
  }

void loop() 
  {  
  for(int x = 0; x < 7; x++) 
    {  
    Serial.print((float) x + random(0, 1000)/1000.0 , 3);   
    Serial.print(',');
    }
  Serial.println();  
  delay(50);  // Make this longer while testing code
  }

A typical working example integrated with your code:

Do some exploration.
Comment\uncomment print statements.
Watch the incoming data on the console while it is running.
Do you see where the null shows up? And why?

Can you comment on each step and explain what is happening?
And why it was done?

What does Serial.println() do? Show the Arduino reference for this and comment on characters sent.

Why did I use trim()? Was it needed for this example?

One of many references:
How to Fix NullPointerException

:)

hello @glv thank you for triggering me :grinning:,
I’ve done an exploration using the Arduino code you gave, and comment/uncomment the print on the processing statement. Based on the results I got, everything is the same as shown in the image below.
image

for Serial.println() is print data to console as a newline

But why null shows up, i still don’t understand.

I try with a combination like this, there is no null, is it clear?

hello @glv and @RichardDL thank you for triggering me, I will move on to the next stage of my project.
I think I can solve the nullpointer problem that I encountered in the previous code. Here are the results and the latest code that I made

//Simple Dyno Logger

import processing.serial.*;
import controlP5.*;


Serial serial;
ControlP5 cp5;

String val;

int speed = 9600;
String portName;
String filename = "contoh";
Table table;

//Variabel Date
int d = day();
int m = month();
int y = year();

//Variable Time
int h = hour();
int min = minute();
int s = second();

//Variable value from serial
float rpm1;
float rpm2;
float torsi;
float daya;
float load;
float tinlet;
float texhaust;
float temp;
float hum;

//variable panjang lengan dyno
float l = 0.152;


void setup() {
  size(600, 400);
  cp5 = new ControlP5(this);

  //make a button for CP
  cp5.addButton("refresh")
    .setPosition(110, 0)
    .setSize(50, 30);
  cp5.addButton("openPort")
    .setPosition(170, 0)
    .setSize(50, 30);
  cp5.addButton("closePort")
    .setPosition(230, 0)
    .setSize(60, 30);
  cp5.addButton("ledOn")
    .setPosition(300, 0)
    .setSize(50, 30);
  cp5.addButton("ledOff")
    .setPosition(360, 0)
    .setSize(50, 30);
  cp5.addScrollableList("comlist")
    .setPosition(0, 0)
    .setSize(100, 150)
    .setBarHeight(30)
    .setItemHeight(30).close();

  //Buat table penyimpanan data
  table = new Table();

  table.addColumn("Date", Table.STRING);
  table.addColumn("Nomor", Table.INT);
  table.addColumn("Time", Table.STRING);
  table.addColumn("RPM Roller", Table.FLOAT);
  table.addColumn("RPM Engine", Table.FLOAT);
  table.addColumn("Torsi (N.m)", Table.FLOAT);
  table.addColumn("Daya (HP)", Table.FLOAT);
  table.addColumn("Load (Kg)", Table.FLOAT);
  table.addColumn("Tinlet (°C)", Table.FLOAT);
  table.addColumn("Texhaust (°C)", Table.FLOAT);
  table.addColumn("Temp (°C)", Table.FLOAT);
  table.addColumn("Humidity (%)", Table.FLOAT);

  TableRow newRow = table.addRow();

  newRow.setString("Date", str(d) + "/" + str (m) + "/" + str(y));
}

void refresh() {
  String list[] = Serial.list();
  cp5.get(ScrollableList.class, "comlist").addItems(list);
}
void comlist(int n) {
  portName = Serial.list()[n];
}
void openPort() {
  serial = new Serial(this, portName, speed);
}
void closePort() {
  serial.stop();
}
void ledOn() {
  serial.write('n');
}
void ledOff() {
  serial.write('f');
}

void draw() {
  background(120);

  //Take data from serial
  if (serial != null) {
    if (serial.available() > 0)
    {
      String val = serial.readStringUntil('\n');

      if (val != null)
      {
        val = val.trim();
        String [] data = split(val, ',');
        print("data : ");
        if (data.length >= 7)
        {
          printArray (data);
        }
        rpm1 = float(data[0]);
        rpm2 = float(data[1]);
        load = float(data[2]);
        tinlet = float(data[3]);
        texhaust = float(data[4]);
        temp = float(data[5]);
        hum = float(data[6]);

        //Data Torsi dan Daya
        torsi = (rpm2 * l)/100;
        daya  = (rpm2 * torsi)/100;
      }

      //Make rows for each data
      TableRow newRow = table.addRow();

      newRow.setInt("Nomor", table.getRowCount());
      newRow.setString("Time", str(h) + ":" + str(min) + ":" + str(s));
      newRow.setFloat("RPM Roller", rpm1);
      newRow.setFloat("RPM Engine", rpm2);
      newRow.setFloat("Torsi (N.m)", torsi);
      newRow.setFloat("Daya (HP)", daya);
      newRow.setFloat("Load (Kg)", load);
      newRow.setFloat("Tinlet (°C)", tinlet);
      newRow.setFloat("Texhaust (°C)", texhaust);
      newRow.setFloat("Temp (°C)", temp);
      newRow.setFloat("Humidity (%)", hum);

      // saveTable(table, "data/new2.csv");
    }

    //make a rect as a value box
    stroke(255);
    fill(0, 50, 100);
    rect(10, 80, 40, 40, 4);
    rect(60, 160, 40, 40, 4);
    rect(110, 80, 40, 40, 4);
    rect(160, 160, 40, 40, 4);
    rect(210, 80, 40, 40, 4);
    rect(260, 160, 40, 40, 4);
    rect(310, 80, 40, 40, 4);
    rect(360, 160, 40, 40, 4);
    rect(410, 80, 40, 40, 4);

    //make a lable for each value
    PFont f = createFont ("Georgia", 15);
    textFont (f);
    text("Rpm Roll", 0, 70);
    text("Rpm Engine", 100, 70);
    text("Torsi (N.m)", 200, 70);
    text("Daya (HP)", 300, 70);
    text("Load (Kg)", 400, 70);
    text("Tinlet (°C)", 50, 150);
    text("Texhaust (°C)", 150, 150);
    text("Temp. (°C)", 250, 150);
    text("Humidity (%)", 350, 150);

    //write value on rect
    int x = 105;
    int y = 185;
    PFont t = createFont ("arial", 15);
    textFont (t);
    fill(255);
    text(rpm1, 15, x);
    text(rpm2, 115, x);
    text(torsi, 220, x);
    text(daya, 320, x);
    text(load, 420, x);
    text(tinlet, 70, y);
    text(texhaust, 170, y);
    text(temp, 270, y);
    text(hum, 370, y);

    //Write SIMPLE DYNOTEST
    stroke(255);
    textFont(f);
    textSize(60);
    fill(255, 255, 255);
    text("SIMPLE DYNOTEST", 20, 300);
  }
}

image