# How to remove Null exception Error

**URL:** <https://discourse.processing.org/t/how-to-remove-null-exception-error/19533>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [April 8, 2020, 8:24am UTC](https://discourse.processing.org/t/how-to-remove-null-exception-error/19533 "2020-04-08T08:24:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KGS](https://avatars.discourse-cdn.com/v4/letter/k/90ced4/32.png) [@KGS](https://discourse.processing.org/u/KGS)\
**Post date:** [April 8, 2020, 8:24am UTC](https://discourse.processing.org/t/how-to-remove-null-exception-error/19533/1 "2020-04-08T08:24:57Z")

</div>

I am getting an error while running the code collecting data from 4 Arduino boards.

> java.lang.NullPointerException  
> at Dual\_IMP\_SPO2.Impedance(Dual\_IMP\_SPO2.java:343)  
> at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)  
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)  
> at java.lang.reflect.Method.invoke(Method.java:498)  
> at processing.core.PApplet.method(PApplet.java:3867)  
> at processing.core.PApplet$2.run(PApplet.java:3905)

How can I solve this error?? The code is given below.

---

<div class="post-metadata">

**Author:** ![KGS](https://avatars.discourse-cdn.com/v4/letter/k/90ced4/32.png) [@KGS](https://discourse.processing.org/u/KGS)\
**Post date:** [April 8, 2020, 8:32am UTC](https://discourse.processing.org/t/how-to-remove-null-exception-error/19533/2 "2020-04-08T08:32:06Z")

</div>

```auto
import processing.serial.*;
import grafica.*;
import controlP5.*;
import g4p_controls.*;
import signal.library.*;

PrintWriter output; // save into a file
PrintWriter output2; // save into a file
PrintWriter output3; // save into a file
PrintWriter output4; // save into a file
PrintWriter output5; // save into a file

Serial Port1; // create a serial port object
String val; // Serial port reading

Serial Port2; // create a serial port object
String val2; // Serial port reading

Serial Port3; // create a serial port object
String val3; // Serial port reading

Serial Port4; // create a serial port object
String val4; // Serial port reading

int nPoints = 50000; // number of points in the array to be saved Impedance of 50 kHz @ 50 Hz sampling rate

public String timestamp = str(hour()) + str(minute()) + str(second());
public int i=0, j=0, k=0, m=0;
public GPlot plot1, plot2, plot3, plot4;

public GPointsArray points = new GPointsArray(nPoints); // create output saving array
public GPointsArray points1 = new GPointsArray(nPoints);

public GPointsArray points2 = new GPointsArray(nPoints);
public GPointsArray points3 = new GPointsArray(nPoints);

public GPointsArray points4 = new GPointsArray(nPoints);
public GPointsArray points5 = new GPointsArray(nPoints);

public GPointsArray points6 = new GPointsArray(nPoints);

ControlP5 cp5; //create controller object

PFont font; //create font object

SignalFilter myFilter; // Create the filter

// Main OneEuroFilter parameters
float freq =120;
float minCutoff = 3.0; // decrease this to get rid of slow speed jitter
float beta = 0.007; // increase this to get rid of high speed lag
float filteredSignal;
String signal;

void setup() {
  size(1500, 1000); // canvas size
  smooth();
  noStroke();
  frameRate(60); 

  output = createWriter( "ReadingIMP.csv" ); // data saving file name
  output2 = createWriter( "ReadingSPO2.csv" ); // data saving file name
  output4 = createWriter( "ReadingTemp.csv" ); // data saving file name
  output5 = createWriter( "ReadingPAT.csv" ); // data saving file name
  output3=createWriter("Indexlist.csv");

  String ports[]=Serial.list(); // finding available serial ports
  for (String port : ports) {
    println(port);
  }

  Port1=new Serial(this, "COM9", 250000); // Open the serial port
  Port1.bufferUntil('\n');
  Port2=new Serial(this, "COM10", 250000); // Open the serial port
  Port2.bufferUntil('\n');
  Port3=new Serial(this, "COM11", 250000); // Open the serial port
  Port3.bufferUntil('\n');
  Port4=new Serial(this, "COM5", 250000); // Open the serial port
  Port4.bufferUntil('\n');

  cp5= new ControlP5(this);
  font =createFont("calibri light", 18);

  cp5.addButton ("Start") //label the button
    .setPosition(1300, 50) //position of the button
    .setSize(100, 80) //width and height
    .setFont(font)
    .setColorBackground(color(50, 50, 255))
    .setColorForeground(color(50, 100, 200))
    // .setColorValue(color(255,255,0))
    .setColorActive(color(50, 150, 150));

  cp5.addButton ("Baseline") //label the button
    .setPosition(1300, 150) //position of the button
    .setSize(100, 80) //width and height
    .setFont(font)
    .setColorBackground(color(255, 150, 50))
    .setColorForeground(color(200, 150, 50))
    .setColorActive(color(150, 100, 150));

  cp5.addButton ("Occlusion") //label the button
    .setPosition(1300, 250) //position of the button
    .setSize(100, 80) //width and height
    .setFont(font)
    .setColorBackground(color(255, 0, 0))
    .setColorForeground(color(200, 50, 50))
    .setColorActive(color(150, 100, 150));

  cp5.addButton ("Release") //label the button
    .setPosition(1300, 350) //position of the button
    .setSize(100, 80) //width and height
    .setFont(font)
    .setColorBackground(color(0, 255, 50))
    .setColorForeground(color(50, 200, 50))
    .setColorActive(color(50, 150, 150));

  cp5.addButton ("Stop") //label the button
    .setPosition(1300, 450) //position of the button
    .setSize(100, 80) //width and height
    .setFont(font)
    .setColorBackground(color(100, 50, 255))
    .setColorForeground(color(50, 100, 200))
    .setColorActive(color(50, 150, 150));

  // Initialize the filter
  myFilter = new SignalFilter(this,1);
  myFilter.setFrequency(freq);
  myFilter.setMinCutoff(minCutoff);
  myFilter.setBeta(beta);

  plot1 = new GPlot(this); // ploting the graph for Impedance 5 kHz
  plot1.setPos(50, 50);
  plot1.setDim(400, 250);
  plot1.setAxesOffset(0);
  plot1.getTitle().setText("Impedance @ 5 kHz and 50 kHz");
  plot1.getXAxis().getAxisLabel().setText("Number of Samples");
  plot1.getYAxis().getAxisLabel().setText("Ohms");
  plot1.setYLim(300, 3000);
  plot1.setFixedYLim(true);
  plot1.setLineColor(color(0, 0, 255));

  plot2 = new GPlot(this); // ploting the graph for SPO2
  plot2.setPos(700, 50);
  plot2.setDim(400, 250);
  plot2.setAxesOffset(0);
  plot2.getTitle().setText("IR and Red Raw signal");
  plot2.getXAxis().getAxisLabel().setText("Number of Samples");
  plot2.getYAxis().getAxisLabel().setText("Absorption Value");
  // plot2.setYLim(60000, 120000);
  // plot2.setFixedYLim(true);
  plot2.setLineColor(color(0, 0, 255));

  plot3 = new GPlot(this); // ploting the graph for Body Temperature
  plot3.setPos(50, 450);
  plot3.setDim(400, 250);
  plot3.setAxesOffset(0);
  plot3.getTitle().setText("Body Temperature and Ambient Temperature");
  plot3.getXAxis().getAxisLabel().setText("Number of Samples");
  plot3.getYAxis().getAxisLabel().setText("Celcius");
  plot3.setYLim(25, 37);
  plot3.setFixedYLim(true);
  plot3.setLineColor(color(255, 0, 0));

  plot4 = new GPlot(this); // ploting the graph for Arterial Pressure
  plot4.setPos(700, 450);
  plot4.setDim(400, 250);
  plot4.setAxesOffset(0);
  plot4.getTitle().setText("Arterial Pressure");
  plot4.getXAxis().getAxisLabel().setText("Number of Samples");
  plot4.getYAxis().getAxisLabel().setText("mmHg");
  plot4.setYLim(60, 90);
  plot4.setFixedYLim(true);
  plot4.setLineColor(color(255, 0, 255));

  while (Port1.available()>0) {
    val=Port1.readStringUntil('\n'); 

    //delay(10);
    if (val!=null) {
      println(val);
      // delay(10);
    }
  }
  while (Port2.available()>0) {
    val2=Port2.readStringUntil('\n'); 

    //delay(10);
    if (val2!=null) {
      println(val2);
      // delay(10);
    }
  }

  while (Port3.available()>0) {
    val3=Port3.readStringUntil('\n'); 

    //delay(10);
    if (val3!=null) {
      println(val3);
      // delay(10);
    }
  }

  while (Port4.available()>0) {
    val4=Port4.readStringUntil('\n'); 

    //delay(10);
    if (val4!=null) {
      println(val4);
      // delay(10);
    }
  }
}

void draw() {

  background(150);

  thread("PAT");
  thread("Impedance");
  thread("SPO2");
  thread("Temperature");

  plot1.setPoints(points); // ploting the graph for Impedance 5 kHz
  plot1.addLayer("layer 4", points1);
  plot1.getLayer("layer 4").setLineColor(color(0, 200, 0));
  plot1.getLayer("layer 4").setLineWidth(3);
  plot1.beginDraw();
  plot1.drawBackground();
  plot1.drawXAxis();
  plot1.drawYAxis();
  plot1.drawTitle();
  plot1.drawGridLines(GPlot.BOTH);
  plot1.drawLines();
  plot1.setLineColor(color(0, 0, 255));
  plot1.setLineWidth(3);
  plot1.activatePanning();
  plot1.activateZooming(2, CENTER, CENTER); 
  plot1.endDraw();
  plot1.removeLayer("layer 4");

  plot2.setPoints(points2); // ploting the graph for SPO2
  plot2.addLayer("layer 2", points3);
  plot2.getLayer("layer 2").setLineColor(color(100, 100, 200));
  plot2.getLayer("layer 2").setLineWidth(3);
  plot2.beginDraw();
  plot2.drawBackground();
  plot2.drawXAxis();
  plot2.drawYAxis();
  plot2.drawTitle();
  plot2.drawGridLines(GPlot.BOTH);
  plot2.drawLines();
  plot2.setLineColor(color(255, 0, 0));
  plot2.setLineWidth(3);
  plot2.activatePanning();
  plot2.activateZooming(2, CENTER, CENTER); 
  plot2.endDraw();
  plot2.removeLayer("layer 2");

  plot3.setPoints(points4); // ploting the graph for Temperature
  plot3.addLayer("layer 3", points5);
  plot3.getLayer("layer 3").setLineColor(color(100, 100, 200));
  plot3.getLayer("layer 3").setLineWidth(3);
  plot3.beginDraw();
  plot3.drawBackground();
  plot3.drawXAxis();
  plot3.drawYAxis();
  plot3.drawTitle();
  plot3.drawGridLines(GPlot.BOTH);
  plot3.drawLines();
  plot3.setLineColor(color(255, 0, 0));
  plot3.setLineWidth(3);
  plot3.activatePanning();
  plot3.activateZooming(2, CENTER, CENTER); 
  plot3.endDraw();
  plot3.removeLayer("layer 3");
  
  
  plot4.setPoints(points6); // ploting the graph for Arterial Pressure
  plot4.beginDraw();
  plot4.drawBackground();
  plot4.drawXAxis();
  plot4.drawYAxis();
  plot4.drawTitle();
  plot4.drawGridLines(GPlot.BOTH);
  plot4.drawLines();
  plot4.setLineColor(color(255, 0, 255));
  plot4.setLineWidth(3);
  plot4.activatePanning();
  plot4.activateZooming(2, CENTER, CENTER);  
  plot4.endDraw();
}

void Impedance() {

  while (Port1.available()>0) {
    // delay(1);
    val=Port1.readStringUntil('\n'); 

    if (val!=null) {
      String timestamp = str(hour()) + str(minute()) + str(second());
      String[] list=split(val, ',');
      i=i+1;
      points.add(i, float(list[0]));
      points1.add(i, float(list[3]));
      output.println(timestamp + ", " + list[0]+ ", " + list[1]+", " + list[2]+", " + list[3]+", " + list[4]+", " + list[5]+
        ", " + list[6]+", " + list[7]+", " + list[8]+", " + list[9]+", " + list[10]+", " + list[11]);
    }
  }
}

void SPO2() {
  while (Port2.available()>0) {
    //delay(50);
    val2=Port2.readStringUntil('\n'); 
    if (val2!=null) {
      val2 = trim(val2);
      String timestamp = str(hour()) + str(minute()) + str(second());
      String[] list=split(val2, ',');

      j=j+1;
      points2.add(j, float(list[0]));
      points3.add(j, float(list[1]));
      output2.println(timestamp + ", " + list[0]+ ", " + list[1]);
    }
  }
}

void Temperature() {

  while (Port3.available()>0) { // Temperature reading
    val3=Port3.readStringUntil('\n'); 
    if (val3!=null) {
      val3 = trim(val3);
      String timestamp = str(hour()) + str(minute()) + str(second());
      String[] list=split(val3, ',');
      k=k+1;
      points4.add(k, float(list[0]));
      points5.add(k, float(list[1]));
      output4.println(timestamp + ", " + list[0]+ ", " +list[1]);
    }
  }
}

void PAT() {

  while(Port4.available()>0) { // PAT signal reading
    //delay(1);
    val4=Port4.readStringUntil('\n'); 

    if (val4!=null && val4.charAt(0)=='#') {
      val4 = trim(val4);
      String timestamp = str(hour()) + str(minute()) + str(second());
      String[] list1=split(val4, '#');
      //String list1=val4;
      // println(timestamp);
      // println(list1[1]);
      m=m+1;
      filteredSignal = myFilter.filterUnitFloat(float(list1[1]));
      //points6.add(m, filteredSignal);
      signal=nf(filteredSignal,0,0);
      //points6.add(m, filteredSignal);
      //points6.add(m, float(list1[1]));
      points6.add(m, float(signal));
      output5.println(timestamp +", " + list1[1]);
    }
  }
}

void Start() {
  Port1.write('%');
  Port2.write('%');
  Port3.write('%');
  Port4.write('%');
}

void Baseline() {
  String timestamp = str(hour()) + str(minute()) + str(second());
  output3.println(timestamp + ","+ i+ ","+ j+ ","+ k+ ","+ m);
  // output1.println(timestamp + ","+ i+ ","+ j+ ","+ k+ ","+ m);
}

void Occlusion() {
  String timestamp = str(hour()) + str(minute()) + str(second());
  output3.println(timestamp +","+ i+ ","+ j+ ","+ k+ ","+ m);
  // output1.println(timestamp + ","+ i+ ","+ j+ ","+ k+ ","+ m);
}

void Release() {
  String timestamp = str(hour()) + str(minute()) + str(second());
  output3.println(timestamp +","+ i+ ","+ j+ ","+ k+ ","+ m);
  //output1.println(timestamp + ","+ i+ ","+ j+ ","+ k+ ","+ m);
}

void Stop () {
  output.flush(); // Writes the remaining data to the file
  output.close(); // Finishes the file
  output2.flush(); // Writes the remaining data to the file
  output2.close(); // Finishes the file
  output3.flush(); // Writes the remaining data to the file
  output3.close(); // Finishes the file
  output4.flush(); // Writes the remaining data to the file
  output4.close(); // Finishes the file
  output5.flush(); // Writes the remaining data to the file
  output5.close(); // Finishes the file
  exit(); // Stops the program
}

```

---

<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:** [April 8, 2020, 10:30am UTC](https://discourse.processing.org/t/how-to-remove-null-exception-error/19533/3 "2020-04-08T10:30:00Z")

</div>

In which line number and which value does the error occur?

Use println to check the values

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [April 8, 2020, 11:18am UTC](https://discourse.processing.org/t/how-to-remove-null-exception-error/19533/4 "2020-04-08T11:18:21Z")

</div>

For a start remove the 4 thread statements from draw to the end of setup. At the moment you are creating multiple threads every time draw is executed that’s 60 times a second.
