0

I created a client/server program where the server sends a packet to the client which has a randomly picked string from an array. But when ever I run the server and then the client which send data about it's byte array the server after receiving the packet throws an exception which looks like this:

Exception in thread "SDServerThread" java.lang.NullPointerException
        at SDServerThread.run(SDServerThread.java:56)

This exception points to this line of code:

buf = GetText.getBytes();

The buf is a byte array list:

byte[] buf = new byte[256];

GetText is a string which calls a method to pick a random string.:

String GetText = getText();

The code starting from byte[] buf = new byte[256]; to buf = GetText.getBytes(); looks like this:

byte[] buf = new byte[256];         
DatagramPacket packet = null;
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
System.out.println("packet received");
textArea.append("packet received\n");

String GetText = getText();
buf = GetText.getBytes();

I haven't tried a lot because i can't find any fixes relating to my kind of problem.

Thanks in advance for the help.

Oscar K.
  • 71
  • 8
  • 1
    Presumably `GetText` is null. Note that using the parameterless `String.getBytes()` method is not recommended; I'd suggest using one with a `Charset` instead, so you're not just using the platform-default encoding. – Jon Skeet Aug 25 '19 at 18:10
  • 1
    As another side note, it would be cleaner to declare and initialize `packet` together: `DatagramPackate packet = new DatagramPacket(buf, buf.length);` – Jon Skeet Aug 25 '19 at 18:10

0 Answers0