我有一个应用程序,我必须列出与Arduino连接的可用端口。我正在使用RxTx。我在当前代码中遇到问题: 在我的GUI中,我从这一行收到所有端口为ArrayList
public ArrayList<String> getPortIdentifiers() {
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
ArrayList<String> ports = new ArrayList<String>();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
ports.add(currPortId.getName());
}
return ports;
}
在我的控制器中,我得到了这个结果,并将结果设置为视图
this.view.setListPorts(listPorts);
在我看来,我得到了ArrayList并将数组设置为ComboBox
new JComboBox(this.listPorts.toArray());
public void setListPorts(ArrayList<String> ports) {
this.listPorts = ports;
}
但是我在这个行号上收到错误
线程中的异常“AWT-EventQueue-0”java.lang.NullPointerException
答案 0 :(得分:1)
以下是更多代码:
public class CommunicationController implements ActionListener {
public CommunicationController() throws IOException, TooManyListenersException {
// initialize connection here
this.communication = new TestComunication();
ArrayList<String> listPorts = this.communication.getPortIdentifiers();
this.view.setListPorts(listPorts);
}
}
...
public class CommunicationView extends JPanel {
private JComboBox portsList;
private ArrayList<String> listPorts;
public CommunicationView() {
....
.....
this.portsList = new JComboBox(this.listPorts.toArray());
}
public void setListPorts(ArrayList<String> ports) {
this.listPorts = ports;
}
}
.....
public class TestComunication {
public ArrayList<String> getPortIdentifiers() {
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
ArrayList<String> ports = new ArrayList<String>();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
ports.add(currPortId.getName());
}
return ports;
}
}
错误