Note that there are some explanatory texts on larger screens.

plurals
  1. PONull Pointer Exception when accessing the serial port
    primarykey
    data
    text
    <p>I have a Java project (in NetBeans 7.1) and I am receiving the following <code>NullPointerException</code>:</p> <pre><code>run: Port COM10 not found. The serial port you are trying to use is currently in usejava.lang.NullPointerException Exception in thread "main" java.lang.NullPointerException at mateorssms.Communicator.ListenOnPort(Communicator.java:68) at mateorssms.Communicator.&lt;init&gt;(Communicator.java:35) at mateorssms.MateorsSMS.main(MateorsSMS.java:14) Java Result: 1 BUILD SUCCESSFUL (total time: 1 second). </code></pre> <p>So my MateorsSMS class is:</p> <pre><code>package mateorssms; import java.awt.Frame; public class MateorsSMS{ public static void main(String[] args) { //UserInterface UI = new UserInterface(); //UI.setVisible(true); new Communicator(); // TODO code application logic here } } </code></pre> <p>and Communicator class (different class in a different file) is</p> <pre><code>package mateorssms; import java.io.IOException; import java.io.InputStream; import javax.comm.*; import java.util.Enumeration; import java.util.TooManyListenersException; public class Communicator implements Runnable, SerialPortEventListener{ static Enumeration portList; static CommPortIdentifier portId; boolean portFound = false; SerialPort serialPort; String defaultPort = "COM10"; Thread readThread; InputStream inputStream; public Communicator(){ getPort(); ListenOnPort(); } private void getPort(){ ////////////////////////////////////////////////////////////////////////////// portFound = false; portList = CommPortIdentifier.getPortIdentifiers(); while(portList.hasMoreElements() &amp;&amp; !(portFound)){ portId = (CommPortIdentifier) portList.nextElement(); if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){ if(portId.getName().equals(defaultPort)){ System.out.println("Found port: " + defaultPort); portFound = true; } } } if(!portFound){ System.err.println("Port " + defaultPort + " not found."); //user feedback //System.exit(1); } } private void ListenOnPort(){ try { serialPort = (SerialPort) portId.open("MateorsSMSApp", 300); System.out.println("yes"); } catch (Exception e) { System.out.println("The serial port you are trying to use is currently in use"+e.toString()); } try { inputStream = serialPort.getInputStream(); System.out.println(inputStream.toString()); } catch (IOException e) { System.err.println("Ex"); } try { serialPort.addEventListener(this); } catch (TooManyListenersException e) { System.err.println("Ex"); } // activate the DATA_AVAILABLE notifier serialPort.notifyOnDataAvailable(true); try { serialPort.setSerialPortParams(460800, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); serialPort.setDTR(true); serialPort.setRTS(true); } catch (UnsupportedCommOperationException e) { System.err.println("Ex"); } readThread = new Thread(this); readThread.start(); } @Override public void serialEvent(SerialPortEvent event) { switch (event.getEventType()) { case SerialPortEvent.BI: System.out.println("BI"); // Break interruptbreak; case SerialPortEvent.OE: System.out.println("OE");// Overrun error break; case SerialPortEvent.FE: System.out.println("FE");// Framing error break; case SerialPortEvent.PE: System.out.println("PE");// Parity error break; case SerialPortEvent.CD: System.out.println("CD");//Carrier detected break; case SerialPortEvent.CTS: System.out.println("CTS");//Clear to send break; case SerialPortEvent.DSR: System.out.println("DSR");// Data set ready break; case SerialPortEvent.RI: System.out.println("RI");// Ring indicator break; case SerialPortEvent.OUTPUT_BUFFER_EMPTY: System.out.println("OUTPUT_BUFFER_EMPTY"); //break; //break; //Buffer empty case SerialPortEvent.DATA_AVAILABLE: //Data Available to be read System.out.println("DATA_AVAILABLE"); byte[] readBuffer = new byte[20]; try { while (inputStream.available() &gt; 0) { int numBytes = inputStream.read(readBuffer); } String result = new String(readBuffer); System.out.println(result); } catch (IOException e) { System.err.println("Ex"); } break; } //SerialPort port; // TODO do something with ev } @Override public void run(){ try { Thread.sleep(3000); } catch (InterruptedException ex){ System.err.println("Ex"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload