RMI Client - ClassCastException error
Hi,
This is my first code in RMI, when I tried to run the server I faced similar problem but managed to run it later, but my client code still giving problem, and this what I'm getting as exception, I'm running my client with following JVM option:
-Djava.security.policy=E:\Gopi\Works\FreeTalkClient\src\sec.all
That security file gives enough previlages to run the client.
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.main.TalkServerImpl
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:84)
at freetalkclient.Main.send(Main.java:40)
at freetalkclient.ChatWindow.jBSend_Click(ChatWindow.java:95)
at freetalkclient.ChatWindow.access$000(ChatWindow.java:15)
at freetalkclient.ChatWindow$1.actionPerformed(ChatWindow.java:50)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
Need help....
# 1
You provided almost no information about the classes in your application, so it's hard to help you. But at a guess, I would say that you need to return your server reference cast to the interface name.
# 2
Server classes :
FreeTalkServer - Class in which main method is written
TalkServerInt - Interface class for RMI
TalkServerImpl - Implementation class for RMI
and then I used rmic and got skeleton class and stub class.
Client Classes :
ChatWindow - GUI class
Main - Class which invokes RMI.
package freetalkclient;
import com.datatype.FrameFormat;
import com.datatype.TalkServerInt;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class Main {
private String me;
public String getFrom() {
return me;
}
private Main() {
}
public static void main(String[] args) {
final Main m = new Main();
if (System.getSecurityManager() == null) {
System.setSecurityManager(new SecurityManager());
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ChatWindow(m).setVisible(true);
}
});
}
public void send(ChatWindow windows,FrameFormat frame) {
try {
TalkServerInt server = (TalkServerInt) Naming.lookup("rmi://localhost/TalkServer");
server.sendData(frame);
} catch (RemoteException ex) {
ex.printStackTrace();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (NotBoundException ex) {
ex.printStackTrace();
}
}
}
Hope this gives better picture. I haven't shown server classes because it's working fine.. problem with client classes only...
# 3
> TalkServerInt server = (TalkServerInt)
> Naming.lookup("rmi://localhost/TalkServer");
This is correct, but according to the exception, what you really have here is
> TalkServerInt server = (TalkServerImpl)
> Naming.lookup("rmi://localhost/TalkServer");
which is wrong.
ejp at 2007-7-7 14:55:44 >

# 4
Sorry I couldn't understand what to do to correct the error..
Let me show the server's main method ...
public static void main(String args[]) {
if(System.getSecurityManager()==null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
TalkServerImpl server = new TalkServerImpl();
Naming.rebind("//localhost/TalkServer",server);
System.out.println("Server bound..");
}catch(Exception e) {
e.printStackTrace();
}
}
If you don't mind can tell me what needs to be done ....
# 5
Sorry, I was 100% wrong about this.
The exception is telling you that the object you tried to cast was really a TalkServerImpl, and that it can't be cast to a TalkServerInt.
That can only mean that your TalkServerImpl object (a) isn't an exported RMI remote object and (b) doesn't implement TalkServerInt.
Its declaration should generally look like this:
public class TalkServerImpl extends UnicastRemoteObject implements TalkServerInt.
and its constructors must throw RemoteException.
Message was edited by:
ejp
Message was edited by:
ejp
ejp at 2007-7-7 14:55:44 >

# 6
After I have changed it I'm getting a different error this time.
I'm getting Null pointer exception.
public void send(ChatWindow windows,FrameFormat frame) {
try {
TalkServerInt server = (TalkServerInt) Naming.lookup("//localhost/TalkServer");
System.out.println(frame.getTo());
if(server==null) {
System.out.println("Null error");
}else {
server.sendData(frame);
}
} catch (RemoteException ex) {
System.out.println("er1");
ex.printStackTrace();
} catch (MalformedURLException ex) {
System.out.println("er2");
ex.printStackTrace();
} catch (NotBoundException ex) {
System.out.println("er3");
ex.printStackTrace();
}
}
This is the code that sends out the data. Some one help me please.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.main.TalkServerImpl.sendData(TalkServerImpl.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:126)
at com.main.TalkServerImpl_Stub.sendData(Unknown Source)
at freetalkclient.Main.send(Main.java:45)
at freetalkclient.ChatWindow.jBSend_Click(ChatWindow.java:95)
at freetalkclient.ChatWindow.access$000(ChatWindow.java:15)
at freetalkclient.ChatWindow$1.actionPerformed(ChatWindow.java:50)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:231)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1778)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
# 7
You bneed to take some responsibility for tracking down your own errors.The listing tells you that you have a bnull pointer exception at line 43 in TalkServerImpl. So what did you do to cause the null popinter extception? What pointer is null that shouldn't be?
# 8
sorry for not being responsible enough to analyse the error.I was in a mindset that the error was in client side, but It was in server side.Now I was able to make it work...Thanks...
# 9
OK, the source line number should have told you that, also this line in the stack trace: at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
ejp at 2007-7-7 14:55:44 >

# 10
I came to know that the error is in the server code by looking at that line only..But I should have done it before guys showed me that ;)