1

我正在开发一个接受串行输入并将接收到的数据上传到 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");
}
4

1 回答 1

1

我自己找到了这个问题的答案。serialEvent 方法签名中的同步说明符阻止了任何其他线程访问 val 变量。我删除了它,它现在可以正常工作了。

于 2013-04-11T06:52:16.493 回答