gpt4 book ai didi

java - RMI方法调用抛出NPE

转载 作者:行者123 更新时间:2023-12-01 12:34:51 27 4
gpt4 key购买 nike

我有:

  1. GUI 要求用户输入以创建新用户。
  2. 一旦点击提交,就会调用AdminModule类中的createPlayer(抛出RemoteException)。
  3. 我将参数传递给 AdminInterfaceImplementation(抛出 RemoteException)以调用 PlayersData 类中的另一个方法来创建对象。请注意,PlayersData 中的方法不会被调用。

我正在运行 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)

最佳答案

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

关于java - RMI方法调用抛出NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25678650/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com