远程回调方法引发Java NullPointerException - Java RMI

时间:2016-09-07 13:35:19

标签: java nullpointerexception rmi

topicssubs是由远程字符串填充的两个私有ArrayListISubs(接口)

private synchronized void notify(MyMessage msg){
    for(int i=0; i<topics.size(); i++){
        if(topics.get(i).equalsIgnoreCase(msg.getTopic())){
            try {
                subs.get(i).getListener().onMessage(msg);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
{p} notify()publish(MyMessage msg)方法调用,它获取一个远程MyMessage对象

 public boolean publish(MyMessage msg) throws RemoteException {

    if(!total.tryAcquire()){
        System.out.println("Publish respinta");
        return false;
    }

    try {
        concurrent.acquire();

        mutex.acquire();
        FileWriter file = new FileWriter("messaggi.txt",true);
        BufferedWriter bw = new BufferedWriter(file);
        bw.write(msg.getTopic()+": "+msg.getContenuto());
        bw.newLine();
        bw.flush();
        bw.close();
        mutex.release();

        notify(msg);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally{
        concurrent.release();
        total.release();
    }
    return true;
}

每次调用publish()时,我都会遇到以下异常:

  

java.lang.NullPointerException at   server.MessageServer.notify(MessageServer.java:87)at   server.MessageServer.publish(MessageServer.java:61)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   sun.rmi.server.UnicastServerRef.dispatch(未知来源)at   sun.rmi.transport.Transport $ 1.run(未知来源)at   sun.rmi.transport.Transport $ 1.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   sun.rmi.transport.Transport.serviceCall(未知来源)at   sun.rmi.transport.tcp.TCPTransport.handleMessages(未知来源)at   sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.run0(未知   来源)at   sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.lambda $运行$ 0(未知   来自java.security.AccessController.doPrivileged(Native Method)   at sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.run(未知   来自)java.util.concurrent.ThreadPoolExecutor.runWorker(未知   来自java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知   来自)java.lang.Thread.run(未知来源)at   sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(未知   来源)在sun.rmi.transport.StreamRemoteCall.executeCall(未知   来源)在sun.rmi.server.UnicastRef.invoke(未知来源)at   java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(未知   来源)at   java.rmi.server.RemoteObjectInvocationHandler.invoke(未知来源)   在com.sun.proxy。$ Proxy0.publish(未知来源)at   publisher.PublisherThread.run(PublisherThread.java:22)

显然topicssubs在调用publish()之前填充。

编辑:好的,我发现了这个问题,请不要杀了我,我忘了在Subscriber构造函数中初始化MsgListener。

0 个答案:

没有答案