Domanda

I am reading a serial port and writing that to a text area as a scrolling value. I have created the SerialPortReader class which works well. My main code simply calls "updateTextArea()" in order to tell the class where the textarea is. This worked will for a while, but when I start throwing more data into the serial port - say ~100 bytes - Java sens a NullPointerException. If I comment out the "updateTextArea()" call (~line 13), then there is no exception. When I uncomment "updateTextArea", the program works perfectly for about a minute. I suspect that the textarea buffer is filling up, but I don't know how to increase the buffer size OR how to make the new text push the old text out of the buffer.

Printing to console works just fine for - presumably - forever. I have only run it for ~10min, but it hasn't so much as hiccuped.

class SerialPortReader implements SerialPortEventListener {
    byte[] serialData;
    TextArea textArea;

    @Override
    public void serialEvent(SerialPortEvent event) {
        try{
            serialData = serialPort.readBytes();
            for(byte dataReceived: serialData){
                System.out.println("Byte Received: " + dataReceived);
            }

            updateTextArea();
        } catch (SerialPortException ex){
            System.out.println(ex);
        }
    }

    public void initiateTextAreaUpdate(TextArea ta){
        textArea = ta;
    }

    public void updateTextArea(){
        if(textArea != null){
            for(int i = 0; i < serialData.length; i++){
                textArea.appendText(serialData[i] + "\n");
            }
        }
    }
}

The exception:

 Exception in thread "EventThread COM5" java.lang.NullPointerException
    at com.sun.javafx.sg.prism.NGTextHelper$TextAttributes.computeLinePadding(NGTextHelper.java:405)
    at com.sun.javafx.sg.prism.NGTextHelper$TextAttributes.access$200(NGTextHelper.java:292)
    at com.sun.javafx.sg.prism.NGTextHelper.buildTextLines(NGTextHelper.java:2357)
    at com.sun.javafx.sg.prism.NGTextHelper.validateText(NGTextHelper.java:1847)
    at com.sun.javafx.sg.prism.NGTextHelper.getCaretShape(NGTextHelper.java:1435)
    at javafx.scene.text.Text.getDecorationShapes(Text.java:1150)
    at javafx.scene.text.Text.impl_geomChanged(Text.java:757)
    at javafx.scene.text.Text$1.invalidated(Text.java:214)
    at javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:127)
    at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:161)
    at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:67)
    at javafx.scene.text.Text.setText(Text.java:188)
    at com.sun.javafx.scene.control.skin.TextAreaSkin$17.invalidated(TextAreaSkin.java:610)
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:359)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
    at javafx.scene.control.TextInputControl$TextProperty.fireValueChangedEvent(TextInputControl.java:1034)
    at javafx.scene.control.TextInputControl$TextProperty.markInvalid(TextInputControl.java:1038)
    at javafx.scene.control.TextInputControl$TextProperty.invalidate(TextInputControl.java:978)
    at javafx.scene.control.TextInputControl$TextProperty.access$200(TextInputControl.java:950)
    at javafx.scene.control.TextInputControl$1.invalidated(TextInputControl.java:119)
    at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:155)
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:100)
    at javafx.scene.control.TextArea$TextAreaContent.insert(TextArea.java:196)
    at javafx.scene.control.TextInputControl.replaceText(TextInputControl.java:373)
    at javafx.scene.control.TextInputControl.insertText(TextInputControl.java:308)
    at javafx.scene.control.TextInputControl.appendText(TextInputControl.java:298)
    at mcsi.McsiGui$SerialPortReader.updateTextArea(McsiGui.java:489)
    at mcsi.McsiGui$SerialPortReader.serialEvent(McsiGui.java:476)
    at jssc.SerialPort$EventThread.run(SerialPort.java:1096)

Thank you for your insight!

j

È stato utile?

Soluzione

I'm not sure if this is causing the null pointer exception, but you need to update the text area on the FX Application Thread. You should replace

updateTextArea();

with

Platform.runLater(() -> updateTextArea());

or, if you are still using Java 7,

Platform.runLater(new Runnable() {
    @Override
    public void run() {
        updateTextArea();
    }
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top