NullPointerException,我该如何解决? JAVAFX-无FXML

时间:2019-11-14 17:04:14

标签: javafx

我有一个小问题。
在我要发送消息的那一刻,我遇到了一个错误:

var url = $("#url").attr("get-employee-name-url");

某些代码:

java.lang.NullPointerException 
at application.Connection.send(Connection.java:29)
at application.Messages.lambda$1(Messages.java:151)

sendBtn.setOnAction(t - > {
    String gotowyTekst = poleDoWpisywania.getText();
    poleDoWpisywania.clear();
    try {
        connection.send(gotowyTekst);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我该如何解决?
我不知道该在哪里寻找解决方案了。

2 个答案:

答案 0 :(得分:0)

当某些不应为null的null发生时,将发生null指针异常。

没有行号,很难准确说明问题出在哪里,但是根据您的异常消息,connectiongotowyTekstconnection.send(gotowyTekst);中似乎为空:

java.lang.NullPointerException <- the exception itself

at application.Connection.send(Connection.java:29) <- where it occurrs

解决方案是确定确切为null的值,或者使其不为null,或者更改代码以使它可以接受null。

This excellent answer提供了有关查找和解决这些问题的更多信息。

答案 1 :(得分:-2)

我找到了错误,并进行了纠正。

sendBtn.setOnAction(t ->
        {

            String gotowyTekst = poleDoWpisywania.getText();
            poleDoWpisywania.clear();
            poleDoWyswietlania.appendText(gotowyTekst + "\n");

            try
            {
                connection.send(gotowyTekst);
            } catch (Exception e)
            {
                // TODO Auto-generated catch block
                poleDoWyswietlania.appendText("Nie udało się wysłać\n");
            }