Description
Installed last nightly build available for Windows (ARDUINO 1.5.6 BETA 2014.02.19), which include the replacement of RXTX library by JSSC.
With a simple sketch on my Arduino Uno that just writes to serial port every second, when I open the Serial Monitor, after the first string appears, I've got this null pointer exception:
Exception in thread "EventThread COM6" java.lang.NullPointerException
at processing.app.Serial.serialEvent(Serial.java:176)
at jssc.SerialPort$EventThread.run(SerialPort.java:1112)
As the exception indicates, the problem is in the Serial.serialEvent method, when reading the serial port.
Looking at the source code:
public synchronized void serialEvent(SerialPortEvent serialEvent) {
if (serialEvent.isRXCHAR()) {
try {
byte[] buf = port.readBytes();
if (buf.length > 0){
...
if (monitor) {
System.out.print(new String(buf));
}
...
}
} catch (SerialPortException e) {
errorMessage("serialEvent", e);
}
}
}
the null pointer exception happens when port.readBytes() returns null.
Testing this outside Arduino IDE, with a simple Java program listening to the serial port with the JSSC library, 2.8.0, I had the same exception appearing at the same point.
Just by checking that the input buffer contains some bytes with
event.getEventValue()>0
the problem will be avoided, as it seems readBytes() returns null instead of an empty array of bytes when the buffer is empty. So:
public synchronized void serialEvent(SerialPortEvent serialEvent) {
if (serialEvent.isRXCHAR() && event.getEventValue()>0) {
try {
byte[] buf = port.readBytes();
...
if (monitor) {
System.out.print(new String(buf));
}
...
} catch (SerialPortException e) {
errorMessage("serialEvent", e);
}
}
}
Hope it helps,
Jose