-1

I have a little problem.
In the moment I want to send a message I have an error:

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

Some code:

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

and

public void send(Serializable data) throws Exception {
    cThread.out.writeObject(data);
}

How I do fix it?
I don't know where to look for a solution anymore.

fabian
  • 80,457
  • 12
  • 86
  • 114
Paul
  • 25
  • 6

2 Answers2

0

Null pointer exceptions occur when something is null that shouldn't be.

Without your line numbers, it's diffcult to tell exactly where the issue with, but it seems likely that either connection, or gotowyTekst is null in connection.send(gotowyTekst); Based on your exception message:

java.lang.NullPointerException <- the exception itself

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

The solution is to determine exactly what is null, and either make it not null, or change your code sos that it can accept nulls.

This excellent answer has more information on finding and fixing these issues.

J Lewis
  • 462
  • 1
  • 4
  • 15
-2

I found the error and corrected it.

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");
            }
Paul
  • 25
  • 6
  • 1
    This "fix" is more than likely to simply hide the issue. Instead of getting the full info, you simply set the same error message regardless of the actual error... Unless the stacktrace was incorrect or you use some crazy listeners for the text property of your control, this is unlikely to fix the actual error... – fabian Nov 14 '19 at 19:19