gpt4 book ai didi

java - Java Serial对象空指针异常的处理

转载 作者:行者123 更新时间:2023-11-29 06:58:09 32 4
gpt4 key购买 nike

首先,我是 Java 的新手,所以如果这是一个显而易见的答案,我深表歉意。

我正在使用 Java 中的 Processing 来读取来自 USB 端口的输入值。

这是我为获取值而设置的自定义类。我调用 getCurrentValue() 方法,它抛出 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;
}
}

它在 seisPort.available() 的第 40 行抛出错误错误信息:

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

我已经坚持了一段时间,欢迎任何反馈。

最佳答案

看看你的构造函数:

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

请注意,您在方法签名中包含了一个 void 关键字。构造函数没有返回类型,因此这将被视为方法,而不是构造函数。当您创建 Seismograph 的新实例时,这将不会被调用,因此您的seisPort 为空。

要解决此问题,只需删除 void 关键字即可。

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

关于java - Java Serial对象空指针异常的处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30761837/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com