0

I've been experiencing some problems with trying to get a callback to work. It constantly gives me this error:

java.lang.NullPointerException
at Networking.JRNetworking.JRNet_Receiver.onReceive(JRNet_Receiver.java:89)
at Networking.JRNetworking.JRNet_Receiver.run(JRNet_Receiver.java:63)
at java.lang.Thread.run(Unknown Source)

here is the relevant code:

package Networking.JRNetworking;

public interface CallBack {
    public void printId();
    public void onReceive(String message, String IP);
}

The caller (object):

public CallBack callBack ;
public JRNet_Receiver(){}
public JRNet_Receiver(CallBack callback)
{
    setCallBack(callback);
    callBack.printId();
    System.out.println("class: " + callback.getClass().toString());
}
public void setCallBack(CallBack call)
{
    System.out.println("setCallBack has been called!");
    callBack = call;
}

and the void that calls:

private void onReceive(String ReceivedString, String senderIp)
{
    try{
    System.out.println(senderIp + "     " + ReceivedString);

    //THIS IS THE TROUBLING PART
    System.out.println("callBack: " + callBack); //<<<<<<<<<<

    //PC Version!
    callBack.onReceive( //<<<<<
            ReceivedString, 
            senderIp);
    }catch(Exception E)
    {
        E.printStackTrace();
    }
}

and the instantiator and called:

JRNet_Receiver receiver ;

public int Id;
@Override
public void initialize(URL location, ResourceBundle resources)
{
    Id= new Random().nextInt(1000);
    System.out.println(Id);

    receiver = new JRNet_Receiver(this);
    System.out.println("Current class: " + this);
    receivePlayers();
}

public void onReceive(String message, String IP)
{
    System.out.println("I have been called in PlayerlistController!" + message + "    " + IP);
}

I hope you can help me!

  • 1
    Method CloseReceiver is where the NPE originated but its not included in your post. The classes mentioned above don't follow any naming conventions and are difficult to read. – Akshay Gehi Dec 07 '15 at 13:45
  • According to the snippet from the stack trace, the exception is happening at line 79, which is in the `CloseReceiver` (sic) method. As far as I can see you haven't even included that method in your post. Can you include the code that throws the exception, and identify which line is line 79? – James_D Dec 07 '15 at 13:45
  • posted the wrong error message, this is the right one. The strange thing is that when I instantiate it, it works. But in the onReceive() function, it fails. It gives me a nullPointer. – jessin rodenburg Dec 07 '15 at 14:45
  • What do you mean "When I instantiate it, it succeeds?". What is the output from your `System.out.println(...);`. And exactly which line is line 89? – James_D Dec 07 '15 at 15:37

0 Answers0