A
A
Andrey Kostyaev2018-05-16 18:02:56
Java
Andrey Kostyaev, 2018-05-16 18:02:56

Throws a stream error when tab.getId().equals() is applied. Who will tell you what the problem is?

Can anyone help me figure out what the problem is. The method code and error are shown below.

public void getMessage(Response response) {
    String id = response.getFrom();
    for (int i = 0; i < tabPane.getTabs().size(); i++) {
      System.out.println(tabPane.getTabs().get(i).getId());
      Tab tab = tabPane.getTabs().get(i);
      if (tab.getId().equals(id)) {
        VBox vBox = (VBox) tabPane.getTabs().get(i).getContent();
        TextArea textArea = (TextArea) vBox.getChildren().get(0);
        textArea.appendText(response.getFrom() + ": " + response.getMessage());
        break;
      }
    }
  }


Exception in thread "Thread-4" java.lang.NullPointerException
at chat.client.view.ChatController.getMessage(ChatController.java:98)(This is the line if (tab.getId().equals(id)) )
at chat. client.MainApp.onReceiveString(MainApp.java:124)
at chat.client.network.TCPConnection$1.run(TCPConnection.java:37)
at java.lang.Thread.run(Thread.java:748)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Alexandrov, 2018-05-16
@mrdestroii

1) strains "Exception in thread "Thread-4" java.lang.NullPointerException", doesn't everything work from another thread for an hour?
2) I assume that (Response response) flies from another thread? Whether a case is wiped by the moment of check String id?
3) Is there exactly Tab tab = tabPane.getTabs().get(i)?
Recommendations
- the most, here is the MOST, most important, any operations with ui should be carried out ONLY in the javafx thread.
- just tab.getId() doesn't call npe? Just in case, read how it works, but better look at the sources / docs, it is possible that if id is not set, then null will be returned and null does not have an equals method and hence npe.
-Take a debugger in your hands, put a break on the desired line and step by step see what is in which variable.
- a very critical moment, don't make a mess in the form of receiving-processing-drawing in one place, this just creates problems with threads. Hint, take a collection that can be multithreaded, for example, a network thread received a packet and threw it into the collection, after which it works further, from this collection the 2nd thread took the packet and processes it and, according to the result, already, for example, answers back to the client or sends the processed data to another one collection from which this data will be picked up by the javafxui thread and rendered. Those. the network stream received and put into the collection without thinking at all what is there, why and where. The business logic thread already digests this data and decides what, where and how, the ui thread does nothing but only draws new data.
In fact, I screwed up with the flows and hence such jokes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question