Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javafx 我能';我不明白我的错误_Javafx_Nullpointerexception_Mqtt - Fatal编程技术网

Javafx 我能';我不明白我的错误

Javafx 我能';我不明白我的错误,javafx,nullpointerexception,mqtt,Javafx,Nullpointerexception,Mqtt,我有一个JavaFX应用程序,它有一个scheduledExecutorService,正在从服务器请求一些数据。这些数据用于填充ListView,当我单击此列表中的某个元素时,会向服务器发送更多数据的请求,并设置MQTT连接,但几秒钟后,我从一些我不理解、甚至不知道哪个对象指向Null的地方收到一个NullPointerException 以下是错误产生的原因: public class DevicePane extends BorderPane { private final TcpCon

我有一个JavaFX应用程序,它有一个scheduledExecutorService,正在从服务器请求一些数据。这些数据用于填充ListView,当我单击此列表中的某个元素时,会向服务器发送更多数据的请求,并设置MQTT连接,但几秒钟后,我从一些我不理解、甚至不知道哪个对象指向Null的地方收到一个NullPointerException

以下是错误产生的原因:

public class DevicePane extends BorderPane {

private final TcpConnection tcp;
private final MqttConnection mqtt;

private final DeviceListView deviceListView;
private SpecificDevicePane deviceInfoPane;

private String[] topicsArray;

public DevicePane() {
    tcp = new TcpConnection();
    mqtt = new MqttConnection();
    mqtt.connect();
    deviceListView = new DeviceListView();
    deviceListView.getSelectionModel().selectedIndexProperty().addListener(e -> {
        if (topicsArray != null) mqtt.unsubscribeToAll(topicsArray);
        AnswerMessage answer = tcp.sendRequest(new RequestMessage(RQST_DEVICE_INFO, deviceListView.getSelectionModel().getSelectedItem().toString()));
        topicsArray = new String[answer.getParams().size()];
        topicsArray = answer.getParams().toArray(topicsArray);
        mqtt.subscribeToAll(topicsArray);
        deviceInfoPane = new SpecificDevicePane(answer.getParams(), mqtt, tcp);
        ScrollPane scrollPane = new ScrollPane(deviceInfoPane);
        this.setCenter(scrollPane);
    });
    this.setLeft(deviceListView);
    // Tâche planifiée afin de récupérer la liste des objets IOT de la DB.
    ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(1);
    threadPool.scheduleAtFixedRate(() -> {
        refreshDeviceList();
    }, 0, 5, TimeUnit.SECONDS);
}
下面是我得到的错误:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at view.DevicePane.lambda$new$8(DevicePane.java:36)
at view.DevicePane$$Lambda$77/576256602.invalidated(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:349)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyIntegerWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:176)
at javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:142)
at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:113)
at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:147)
at javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:68)
at javafx.scene.control.MultipleSelectionModelBase.clearSelection(MultipleSelectionModelBase.java:665)
at javafx.scene.control.ListView$ListViewBitSetSelectionModel.updateSelection(ListView.java:1306)
at javafx.scene.control.ListView$ListViewBitSetSelectionModel.access$1600(ListView.java:1173)
at javafx.scene.control.ListView$ListViewBitSetSelectionModel$1.onChanged(ListView.java:1246)
at javafx.collections.WeakListChangeListener.onChanged(WeakListChangeListener.java:88)
at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:329)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
at javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
at javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482)
at javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541)
at javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205)
at javafx.collections.ModifiableObservableListBase.setAll(ModifiableObservableListBase.java:90)
at view.DeviceListView.refreshShownDevice(DeviceListView.java:43)
at view.DeviceListView.setDeviceList(DeviceListView.java:33)
at view.DeviceListView.lambda$setDeviceList$7(DeviceListView.java:36)
at view.DeviceListView$$Lambda$160/2054192134.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1826358374.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1915503092.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
第36行是这一行:
AnswerMessage answer=tcp.sendRequest(新的RequestMessage(RQST_DEVICE_INFO,deviceListView.getSelectionModel().getSelectedItem().toString())


我希望有人能帮助我

所选项目是否为空
?是的,我刚刚找出问题所在。当刷新我的ListView时,侦听器事件将第二次触发。我能以某种方式避免吗?只需在侦听器中执行空检查。我在重新填充之前清除了ObservableList。当我简单地添加一些东西时,一切都很好。