nullpointerexception在java中使用串行端口时如何避免null指针异常?
所以,我正在制作一个程序,通过发送命令与传感器进行通信。但是,当我尝试单击GUI中的按钮时,会出现两个错误:nullpointer异常和portinuse异常,这会阻止按钮工作。我该如何解决这个问题
SimpleRead类:
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import javax.comm.*;
import javax.swing.*;
public class SimpleRead {
static Enumeration portList;
static CommPortIdentifier portId;
static PrintStream os;
static BufferedReader is;
static java.util.Timer t = new java.util.Timer();
static TimersTask tt = new TimersTask();
public static void main(String[] args) {
GUI GUI = new GUI();
JFrame window = new JFrame("DI-100");
window.setSize(700, 300);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(GUI);
Enumeration portList = CommPortIdentifier.getPortIdentifiers();
String wantedPortName;
CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
ArrayList<String> serialports = new ArrayList<String>();
serialports.add(portId.getName());
String[] ports = new String[serialports.size()];
ports = serialports.toArray(ports);
GUI.jComboBox2 = new JComboBox(ports);
GUI.jComboBox2.addActionListener(GUI.jComboBox2);
wantedPortName = (String) GUI.jComboBox2.getSelectedItem();
while (portList.hasMoreElements()) {
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL &&
portId.getName().equals(wantedPortName)) {
CommPortIdentifier pid = portId;
SerialPort port = null;
try {
port = (SerialPort) portId.open("OpenPort", 1000);
} catch(PortInUseException e) {
System.err.println("Port already in use: " + e);
}
try {
port.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {
System.out.println("you suck");
}
try {
os = new PrintStream(port.getOutputStream(), true, "ISO-8859-1");
}
catch (IOException e) {
System.err.println("Can't open input stream: write-only");
}
try {
is = new BufferedReader(new InputStreamReader(port.getInputStream()));
} catch (IOException e) {
System.err.println("Can't open input stream: write-only");
}
try{
os = new PrintStream(port.getOutputStream(), true);
} catch (IOException e){
}
}
else {
}
}
}
}
}
TimerTask类:
import java.io.IOException;
import java.util.*;
public class TimersTask extends TimerTask{
@Override
public void run() {
SimpleRead.os.print("W");
try {
String returnvalue = SimpleRead.is.readLine();
System.out.println(returnvalue);
GUI.jTextField1.setText(returnvalue);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
//is.readLine(); // Second read will remove the extra line feed that AT generates as
} catch (IOException e){
}
}
}
# 1 楼答案
好的,我想这就是正在发生的事情。您试图打开端口:
port = (SerialPort) portId.open("OpenPort", 1000);
,但无论出于何种原因,它都会失败。然后继续使用它:port.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
,只是此时它为空,所以程序会爆炸SimpleRead类:
TimersTask类:
这解决了你的一个问题,还有很多你不知道的问题。try/catch的工作方式是,当某个代码失败时,块中的其余代码不会运行,因为它取决于失败的代码。相反,您的代码只需在失败时一个接一个地抛出异常即可
根本的问题是
PortInUseException
,这是不言自明的。其他东西已经在使用该端口。检查你的代码(在这里和其他地方),确保你没有试图打开端口两次(比如,如果你多次点击按钮?),如果这还不能解决问题,你的电脑上可能有另一个程序正在抓取这个端口“记住,孩子们,用完后一定要关闭端口!”