回调nullpointer异常

时间:2015-12-07 13:38:55

标签: java javafx callback

我在试图让回调工作时遇到了一些问题。它经常给我这个错误:

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)

这是相关代码:

package Networking.JRNetworking;

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

来电者(对象):

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;
}

和调用的空白:

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();
    }
}

和实例化器并调用:

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);
}

我希望你能帮助我!

0 个答案:

没有答案