Processing in Java Serial object Null Pointer Exception

743 views Asked by At

First of all I am new to Java so I apologize if this is an obvious answer.

I am using Processing in Java to read the input values coming from a usb port.

Here is the custom class I set up to get the values. I call the getCurrentValue() method and it throws the NullPointerException.

import processing.core.PApplet;
import processing.serial.*; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class Seismograph extends PApplet{

    private int currentValue, verticalOffset = 0;
    private int zero = 32768;                                                                                      // .5 of maximum seismograph value
    private int adjustSeis = 68;                                                                                   // devide seismograph data by adjustSies to scale
    public String theSeisPort = "COM20";
    private Serial seisPort;  

    public void Seismograph(){
      currentValue = 0;
      seisPort = new Serial(this, "COM20", 38400);
    }


    public int getCurrentValue(){
        byte[] inBuffer = new byte[2];                                                                       // value for seis - 2 bytes

        while (seisPort.available() > 0) {                                                                   // verify serial port
          inBuffer = seisPort.readBytes();
          seisPort.readBytes(inBuffer);                                                                      // read bytes into buffer

        if (inBuffer != null)  {
            String seisString = new String(inBuffer);                                                        // capture value
            float seisFloat = PApplet.parseFloat(seisString);                                                             // convert value to float
            int seisInt = PApplet.parseInt((seisFloat-zero)/adjustSeis);                                                  // adjust data to fit screen
            seisInt = seisInt+(height/2)-verticalOffset;                                                     // center data vertically
            currentValue = seisInt;
            }

        }
        return this.currentValue;
    }
}

It throws the error because of line 40 at seisPort.available() The error message:

Exception in thread "Animation Thread" java.lang.RuntimeException: java.lang.NullPointerException
    at com.jogamp.common.util.awt.AWTEDTExecutor.invoke(AWTEDTExecutor.java:58)
    at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:103)
    at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:206)
    at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:172)
    at javax.media.opengl.Threading.invoke(Threading.java:191)
    at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:541)
    at processing.opengl.PJOGL.requestDraw(PJOGL.java:688)
    at processing.opengl.PGraphicsOpenGL.requestDraw(PGraphicsOpenGL.java:1651)
    at vialab.SMT.renderer.PGraphics3DDelegate.requestDraw(PGraphics3DDelegate.java:1450)
    at processing.core.PApplet.run(PApplet.java:2254)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.NullPointerException
    at seismic.Seismograph.getCurrentValue(Seismograph.java:40)
    at seismic.seismograph_jan22c_1_1.draw(seismograph_jan22c_1_1.java:102)
    at processing.core.PApplet.handleDraw(PApplet.java:2384)
    at processing.opengl.PJOGL$PGLListener.display(PJOGL.java:862)
    at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:665)
    at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:649)
    at javax.media.opengl.awt.GLCanvas$10.run(GLCanvas.java:1289)
    at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1119)
    at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:994)
    at javax.media.opengl.awt.GLCanvas$11.run(GLCanvas.java:1300)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:241)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

I have been stuck on this for awhile, any feedback is appreciated.

2

There are 2 answers

1
Kevin Workman On BEST ANSWER

Look at your constructor:

public void Seismograph(){
      currentValue = 0;
      seisPort = new Serial(this, "COM20", 38400);
}

Notice that you've included a void keyword in the method signature. Constructors do not have a return type, so this will be treated as a method instead of a constructor. This will not be called when you create a new instance of Seismograph, so your seisPort is null.

To fix this, simply remove the void keyword.

public Seismograph(){
      currentValue = 0;
      seisPort = new Serial(this, "COM20", 38400);
}
0
Daniel M. On

The constructor shouldn't have void in its signature. Since java doesn't see a valid constructor, it creates an empty one, called the default constructor. Thus, seisPort isn't initialized when an instance is created, and referring to it causes java to throw a NullPointerException.