Reputation: 2834
I have a Java project (in NetBeans 7.1) and I am receiving the following NullPointerException
:
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.<init>(Communicator.java:35)
at mateorssms.MateorsSMS.main(MateorsSMS.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second).
So my MateorsSMS class is:
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
}
}
and Communicator class (different class in a different file) is
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() && !(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() > 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");
}
}
}
Upvotes: 1
Views: 3284
Reputation: 9013
You get three exception messages:
Port COM10 not found.
This message is produced in getPort()
. Probably portId
is still null after returning from that method.
The serial port you are trying to use is currently in usejava.lang.NullPointerException
This message is produced in the first try-block of ListenOnPort()
. See the end "java.lang.NullPointerException" of that message. This supports the claim that portId
ist null.
Exception in thread "main" java.lang.NullPointerException
Now as noted by assylias, the call serialPort.getInputStream()
fails, because serialPort
is null, too.
I suggest you improve the error reporting, e.g. print messages and stack traces of exceptions you catch with e.printStackTrace()
instead of System.err.println("Ex")
.
Upvotes: 0
Reputation: 328737
It looks like this line:
serialPort = (SerialPort) portId.open("MateorsSMSApp", 300);
throws an exception, which probably means that serialPort
is still null after that line because the call to open
failed.
Then on the next try block you have the following line, which would throw a NPE if serialPort
is null:
inputStream = serialPort.getInputStream();
It would make your life easier if you only had one try/catch block in the ListenOnPort
method instead of 3.
Note: method names in Java generally start with a lower case: ListenOnPort
=> listenOnPort
Upvotes: 2