-1

I'm attempting to get a JTextArea to display the output from a COM Port as a string. However, the code i'm using to tell the JTextArea what it should be displaying is producing NullPointerExceptions. I know what's causing the error roughly but i'm just looking for the best way to deal with the issue to prevent it happening. The code for the text areas is:

    if (Serial.currPortId == null) {
        listenState.setText("NONE");
    } else {
        listenState.setText(Serial.currPortId.getName().toString());
    }
    if (Serial.currPortId.getName().toString().equals(Serial.PORT_NAMES[0].toString())) {
        tab1sheet.setText(serialImport.datafeed); }
    else {
        tab1sheet.setText("N/A");
    }
    if (Serial.currPortId.getName().toString().equals(Serial.PORT_NAMES[1].toString())) {
        tab2sheet.setText(serialImport.datafeed); }
    else {
        tab2sheet.setText("N/A");
    }
    if (Serial.currPortId.getName().toString().equals(Serial.PORT_NAMES[2].toString())) {
        tab3sheet.setText(serialImport.datafeed); }
    else {
        tab3sheet.setText("N/A");
    }

The errors are occurring at the start of

 if (Serial.currPortId.getName().toString().equals(Serial.PORT_NAMES[0].toString())) {
    tab1sheet.setText(serialImport.datafeed); }

and I assume the if statements for the other two aren't working either.

I'm also providing the code for the serialports. It is as follows:

public void initialize() {
    CommPortIdentifier portId = null;
    Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

    while (portEnum.hasMoreElements()) {
        CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
        for (String portName : PORT_NAMES) {
            if (currPortId.getName().equals(portName)) {
                portId = currPortId;
                break;
            }
        }
    }
    if (portId == null) {
        System.out.println("Could not find COM port.");
        return;
    }

    try {
        serialPort = (SerialPort) portId.open(this.getClass().getName(),
                TIME_OUT);
        serialPort.setSerialPortParams(DATA_RATE,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);

        // open the streams
        input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
        output = serialPort.getOutputStream();
        String datafeed = "";
        datafeed.concat(input.toString());
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
    } catch (Exception e) {
        System.err.println(e.toString());
    }
}

I've attempted to change the code and nest it but it's still not working. It's now:

if (Serial.currPortId.getName() != null) {
        if (Serial.currPortId.getName().toString().equals(Serial.PORT_NAMES[0].toString())) {
            tab1sheet.setText(serialImport.datafeed); }
        else if (Serial.currPortId.getName().toString().equals(Serial.PORT_NAMES[1].toString())) {
            tab2sheet.setText(serialImport.datafeed); }
        else if (Serial.currPortId.getName().toString().equals(Serial.PORT_NAMES[2].toString())) {
            tab3sheet.setText(serialImport.datafeed); }
        else {tab1sheet.setText("N/A");
              tab2sheet.setText("N/A");
              tab3sheet.setText("N/A");}
    }
Joseph Oliver
  • 169
  • 1
  • 12

1 Answers1

1

Looking at your code:

if (Serial.currPortId == null) {  <--if this is true  
        ...
} else {  
    ... 
}
if (Serial.currPortId.getName()...))) { <-- this is a null pointer null.getName()

use else if, or put everything in the else, or make the first if exit this function.

user207421
  • 305,947
  • 44
  • 307
  • 483
Ofer Skulsky
  • 685
  • 4
  • 10