У меня есть проект Java (в NetBeans 7.1), и я получаю следующее 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).
Итак, мой класс MateorsSMS:
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
}
}
и класс Communicator (другой класс в другом файле) равен
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");
}
}
}