我正在开发一个接受串行输入并将接收到的数据上传到 COSM 服务器的 java 程序。存储从串口接收到的数据的全局变量不会在用于更新 COSM 的方法中重现该值。有人可以告诉我这有什么问题吗?我之前什至使用过全局队列对象,但是当在更新方法中的队列对象上调用 remove 时,NullPointerException
会抛出更新方法。
public synchronized void serialEvent(SerialPortEvent oEvent)//Event handler for serial communication
{
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE)
{
try {
val = input.read();
this.update(78164);
} catch (PachubeException e) {
// If an exception occurs it will print the error message from the
// failed HTTP command
System.err.println(e.errorMessage);
//System.out.println("Main method");
} catch (IOException e) {
System.err.println(e);
// System.out.println("Main method");
} catch (Exception e) {
// System.out.println("Main method");
System.err.println(e);
}
}
val
是全局变量。当在方法中调用时update
,值val
是零或 20(值val
在其定义中被初始化)。这是更新方法的实现:
private void update(int feedid) throws PachubeException, IOException ,Exception
{
Feed f = this.pachube.getFeed(feedid);
System.out.println("updating ...");
f.updateDatastream(8345, (double) val);
f.updateDatastream(6274, (double) val);
f.updateDatastream(1044, (double) val);
// f.updateDatainputDatastream((f.getData(), in, out, true));
//System.out.println(val);
System.out.println("updated");
}