2013-03-23 30 views
1

我正在接受串行输入和上载COSM服务器上接收到的数据的Java程序。存储从串口收到数据的全局变量不会在用于更新COSM的方法中重现该值。请有人告诉我这有什么问题吗? 我甚至使用的全局队列对象之前,但删除时被调用Update方法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"); 
} 
+0

告诉我们如何更新()的实施,以及在何处以及如何VAL声明。 – 2013-03-23 18:04:42

+1

'NullPointerException'正在抛出,因为'f'可能没有被初始化。检查'pachube.getFeed(feedid)'是否返回对'Feed'类的实例的引用。 – 2013-03-23 18:14:16

+0

我不认为这是一个问题,因为当数据被分配到队列对象的更新方法本身值显示在输出控制台 – 2013-03-23 18:30:29

回答

1

我找到了答案,这个问题myself.The同步符中的serialEvent方法的签名访问瓦尔可变防止任何其他线程。我删除它,它现在工作完美。