0

I am trying to print to a TextArea in a different class, I am getting a NullPointerException.

Here is the code that I am writing to the textarea

public class basicinterface extends intGui implements SerialPortEventListener {

/**
 * Stream for the storage of incoming data
 */
private InputStream inputStream;
/**
 * Stream for the dispatching of data
 */
private OutputStream outputStream;
/**
 * Timeout of the USB port
 */
private final int PORT_TIMEOUT = 2000;
/**
 * Representation of the serial port used for the communication
 */
private SerialPort serialPort;
/**
 * Buffer that stores the received bytes from the media
 */
protected LinkedBlockingQueue<Byte> receivedBytes;

/**
 * Builds a new manager for the communication via USB port.
 * @exception IOException if an error occurred during the opening of the USB port
 */
public basicinterface() throws IOException {
  receivedBytes = new LinkedBlockingQueue<Byte>(100000);
    String port = "COM8"; //place the right COM port here, OS dependent

    //Check that the USB port exists and is recognized:
    Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
    boolean portFound = false;
    CommPortIdentifier portId = null;
    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            intGui.termoutput.setText(portId.getName());
            if (portId.getName().equals(port)) {
                intGui.termoutput.setText("Found port: " + port);
                portFound = true;
                break;
            } 
        } 
    } 

    if (!portFound) 
        throw new IOException("port " + port + " not found.");

    try {
        intGui.termoutput.setText("USB port opening...");
        serialPort = (SerialPort) portId.open("USBCommunicator", PORT_TIMEOUT);
        intGui.termoutput.setText("USB port opened");
        inputStream = serialPort.getInputStream();
        outputStream = serialPort.getOutputStream();
        serialPort.addEventListener(this);
                serialPort.notifyOnDataAvailable(true);
        //#==================================================================#
        // WARNING! - DO NOT SET THE FOLLOWING PROPERTY WITH RXTX LIBRARY, IT
        //            CAUSES A PROGRAM LOCK:
        //  serialPort.notifyOnOutputEmpty(true);
        //#==================================================================#

        //wait for a while to leave the time to javax.comm to
        //correctly configure the port:
        Thread.sleep(1000);

        int baudRate = 115200; //set propertly
        serialPort.setSerialPortParams(baudRate, 
            SerialPort.DATABITS_8, 
            SerialPort.STOPBITS_1, 
            SerialPort.PARITY_NONE);

        serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);

        intGui.termoutput.setText("setted SerialPortParams");
    } catch (Exception e) {
        System.err.println(e.getMessage());
        throw new IOException(e.getMessage());
    }
}


public void closeUSB(){
    //close the streams for serial port:
    try {
        inputStream.close();
        outputStream.close();
    } catch (IOException e) {
        System.err.printf("Cannot close streams:" + e.getMessage(), e);
    }
}

/**
 * Listener for USB events
 * 
 * @param event new event occurred on the USB port
 */
public void serialEvent(SerialPortEvent event){
    switch (event.getEventType()) {

        case SerialPortEvent.BI:
        case SerialPortEvent.OE:
        case SerialPortEvent.FE:
        case SerialPortEvent.PE:
        case SerialPortEvent.CD:
        case SerialPortEvent.CTS:
        case SerialPortEvent.DSR:
        case SerialPortEvent.RI:
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            //nothing to do...
            break;

        case SerialPortEvent.DATA_AVAILABLE:
            byte received = -1;
            do {
                try {
                    received = (byte)inputStream.read();
                } catch (IOException e) {
                    System.err.println("Error reading USB:" + e.getMessage());
                }

                synchronized (receivedBytes) {
                    try {
                        receivedBytes.add(received);
                    } catch (IllegalStateException ew) {
                        System.err.println(ew.getMessage());
                        receivedBytes.poll();
                        receivedBytes.add(received);
                    }
                }
            } while(received != -1);

            break;
    }
}

protected void write(byte[] buffer){
    try {
        outputStream.write(buffer);
        outputStream.flush();
    } catch (IOException e) {
        System.err.println("Cannot write:" + e.getMessage());
    }
}
public static void main(String[] args) {
    Thread thread1 = new Thread() {
        public void run() {
            try {
                new basicinterface();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };

    Thread thread2 = new Thread() {
        public void run() {
            new intGui();
        }
    };

    // Start the downloads.
    thread1.start();
    thread2.start();

    // Wait for them both to finish
    try {
        thread1.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        thread2.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();    }
}
}

This is my JTextArea code here

    public class intGui  {
    public static JTextArea termoutput;
    public static JTextField baudrate;
    public static void initialgui() {
        JFrame frame = new JFrame();
        frame.setSize(600,600);
        frame.setVisible(true);

        JPanel panel = new JPanel();
        frame.add(panel);
        panel.setLayout(new GridLayout(2,3));

        baudrate = new JTextField("9600");
        termoutput = new JTextArea("Testing");
        panel.add(baudrate);
    }

    public static void main(String[] args) {

    initialgui();
    }
}

I am a little lost about where to begin with this problem.

Top-Bot
  • 782
  • 1
  • 6
  • 21
  • 1
    Consider providing a runnable **example** which demonstrates your problem. Also, you might like to point out the exact line the NPE occurs, makes it easier to diagnose. `static` is NOT a cross object communication mechanism, based on your limited example, you're either going to be blocking the EDT, preventing any possible updates or violating the single threaded nature of Swing ... possibly both ... but then again, I only have a dozen or lines of code to go by :P – MadProgrammer Jan 17 '17 at 21:04
  • 2
    *"I am a little lost about where to begin with this problem"* - The stack trace, it will point out the line which caused the exception (and how the code got there), from there you need to verify each step and determine when the object should have been properly initialised – MadProgrammer Jan 17 '17 at 21:17
  • Thanks, I figured it out – Top-Bot Jan 17 '17 at 21:27

0 Answers0