nullpointerexception - Processing in Java Serial object Null Pointer Exception -


first of new java apologize if obvious answer.

i using processing in java read input values coming usb port.

here custom class set values. call getcurrentvalue() method , throws 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 0 = 32768;                                                                                      // .5 of maximum seismograph value     private int adjustseis = 68;                                                                                   // devide seismograph data adjustsies 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 seis - 2 bytes          while (seisport.available() > 0) {                                                                   // verify serial port           inbuffer = seisport.readbytes();           seisport.readbytes(inbuffer);                                                                      // read bytes buffer          if (inbuffer != null)  {             string seisstring = new string(inbuffer);                                                        // capture value             float seisfloat = papplet.parsefloat(seisstring);                                                             // convert value float             int seisint = papplet.parseint((seisfloat-zero)/adjustseis);                                                  // adjust data fit screen             seisint = seisint+(height/2)-verticaloffset;                                                     // center data vertically             currentvalue = seisint;             }          }         return this.currentvalue;     } } 

it throws error because of line 40 @ seisport.available() error message:

exception in thread "animation thread" java.lang.runtimeexception: java.lang.nullpointerexception     @ com.jogamp.common.util.awt.awtedtexecutor.invoke(awtedtexecutor.java:58)     @ jogamp.opengl.awt.awtthreadingplugin.invokeonopenglthread(awtthreadingplugin.java:103)     @ jogamp.opengl.threadingimpl.invokeonopenglthread(threadingimpl.java:206)     @ javax.media.opengl.threading.invokeonopenglthread(threading.java:172)     @ javax.media.opengl.threading.invoke(threading.java:191)     @ javax.media.opengl.awt.glcanvas.display(glcanvas.java:541)     @ processing.opengl.pjogl.requestdraw(pjogl.java:688)     @ processing.opengl.pgraphicsopengl.requestdraw(pgraphicsopengl.java:1651)     @ vialab.smt.renderer.pgraphics3ddelegate.requestdraw(pgraphics3ddelegate.java:1450)     @ processing.core.papplet.run(papplet.java:2254)     @ java.lang.thread.run(thread.java:724) caused by: java.lang.nullpointerexception     @ seismic.seismograph.getcurrentvalue(seismograph.java:40)     @ seismic.seismograph_jan22c_1_1.draw(seismograph_jan22c_1_1.java:102)     @ processing.core.papplet.handledraw(papplet.java:2384)     @ processing.opengl.pjogl$pgllistener.display(pjogl.java:862)     @ jogamp.opengl.gldrawablehelper.displayimpl(gldrawablehelper.java:665)     @ jogamp.opengl.gldrawablehelper.display(gldrawablehelper.java:649)     @ javax.media.opengl.awt.glcanvas$10.run(glcanvas.java:1289)     @ jogamp.opengl.gldrawablehelper.invokeglimpl(gldrawablehelper.java:1119)     @ jogamp.opengl.gldrawablehelper.invokegl(gldrawablehelper.java:994)     @ javax.media.opengl.awt.glcanvas$11.run(glcanvas.java:1300)     @ java.awt.event.invocationevent.dispatch(invocationevent.java:241)     @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:733)     @ java.awt.eventqueue.access$200(eventqueue.java:103)     @ java.awt.eventqueue$3.run(eventqueue.java:694)     @ java.awt.eventqueue$3.run(eventqueue.java:692)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76)     @ java.awt.eventqueue.dispatchevent(eventqueue.java:703)     @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:242)     @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:161)     @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:150)     @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:146)     @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:138)     @ java.awt.eventdispatchthread.run(eventdispatchthread.java:91) exception in thread "awt-eventqueue-0" java.lang.nullpointerexception 

i have been stuck on awhile, feedback appreciated.

look @ constructor:

public void seismograph(){       currentvalue = 0;       seisport = new serial(this, "com20", 38400); } 

notice you've included void keyword in method signature. constructors not have return type, treated method instead of constructor. not called when create new instance of seismograph, seisport null.

to fix this, remove void keyword.

public seismograph(){       currentvalue = 0;       seisport = new serial(this, "com20", 38400); } 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -