RMI方法调用抛出NPE

时间:2014-09-05 04:33:48

标签: java rmi

我有:

  1. GUI,要求用户输入以创建新用户。
  2. 点击提交后,它将在AdminModule类中调用createPlayer(抛出RemoteException)。
  3. 我将参数传递给AdminInterfaceImplementation(抛出RemoteException)以调用PlayersData类中的另一个方法来创建对象。请注意,PlayersData中的方法不会被调用。
  4. 我正在运行RMI并使用简单的println测试了连接,它运行正常。但是,该方法仍未调用。请告知可能出现的问题。

    PlayersData中的方法是否需要抛出RemoteException才能工作?

    // AdminModule
    
    public static void main(String args[])
        {
            try {
    
                System.out.println("Hi from AdminModule");
                AdminFactory theWorks = (AdminFactory) Naming.lookup("rmi://localhost:13456/AdminInterfaceTest");
                myAdminInterface = theWorks.newAdminInterface();
                new AdminModule().run();
            }
    
            catch (Exception e)
            {
                System.out.println("catch" + e);
            }
        }
    
        public void createPlayer(String name, String password, int chips) throws RemoteException
            {
                String hashedPassword = Utility.getHash(password);
                myAdminInterface.createPlayer(name, hashedPassword, chips);    
                System.out.println ("Create Player from Admin Module: " + name + ", " + hashedPassword + ", " + chips);
    
            }
    
    
        // AdminInterfaceImplementation
    
        public void createPlayer(String name, String password, int chips) throws RemoteException {
                System.out.println ("Create Player from Admin Interface: " + name + ", " + password + ", " + chips);  // can display in console
                adminModule.test(name, password, chips);  //this method doesn't get invoked.
            }
    
    
    //PlayersData   - not invoke
    
    public void addPlayer(String name, String password, int chips) {
    
            System.out.println ("Create Player from PlayersData: " + name + ", " + password + ", " + chips);
            Player p = new Player (name, password, chips);
            playerList.add(p);
            JOptionPane.showMessageDialog(null, "Player created successfully", "Display", 
                    JOptionPane.INFORMATION_MESSAGE);
    }
    

    更新

    该方法现在被调用,但有以下错误...

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at AdminInterfaceImpl.createPlayer(AdminInterfaceImpl.java:13)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:303)
        at sun.rmi.transport.Transport$1.run(Transport.java:159)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
        at java.lang.Thread.run(Thread.java:695)
        at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
        at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
        at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
        at AdminInterfaceImpl_Stub.createPlayer(Unknown Source)
        at AdminModule.createPlayer(AdminModule.java:79)
        at AdminMenuFrame$6.actionPerformed(AdminMenuFrame.java:198)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
        at java.awt.Component.processMouseEvent(Component.java:6414)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
    

1 个答案:

答案 0 :(得分:1)

通过在方法createPlayer中初始化PlayersData类对象来解决问题。