Hi all,
I'm developing a simple RMI chat server.
Im getting following exception when i execute Client.
Server works well.

java.lang.IllegalArgumentException: argument type mismatch
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
        at sun.rmi.transport.Transport$1.run(Transport.java:177)
        at sun.rmi.transport.Transport$1.run(Transport.java:174)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:722)
        at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
        at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
        at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:160)
        at ServrImplementation_Stub.joinChatRoom(Unknown Source)
        at ClientImplementation.<init>(ClientImplementation.java:28)
        at ClientImplementation.main(ClientImplementation.java:79)

this is my client implementation

 public ClientImplementation(String name) throws RemoteException
    {
            String arg = "rmi://localhost:2828/chatfun";
            myName=name;
           // myGui=gui;
            try{
            csi=(chatServerInterface)Naming.lookup(arg);
            csi.joinChatRoom(name,(chatClientInterface)this);//this is the line which the exception shows
            this.chatStart();
            }catch(Exception ex)
            {
                //JOptionPane.showMessageDialog(null, "Failed\n"+ex.printStackTrace());
                //System.out.println("Failed to bind\n"+ex.printStackTrace());
                ex.printStackTrace();
                System.exit(0);
            }


    }

this is my server interface

public interface chatServerInterface extends Remote{

    public void joinChatRoom(String clientName,chatClientInterface c) throws RemoteException ;
    public void leaveChatRoom(String clientName) throws RemoteException;
    public void sendToAll(String msg,String name) throws RemoteException;
    public java.util.Iterator getOnlineClients() throws RemoteException;


}

PLZZZZZZZZZZz....help me i cannot find the mistake i made.

Have you checked what value is assigned to csi before it calls the joinChatRoom()?

Null is assigned.

public class ClientImplementation{

    chatServerInterface csi = null;

     public ClientImplementation(String name) throws RemoteException
    {
            String arg = "rmi://localhost:2828/chatfun";
            myName=name;
           // myGui=gui;
            try{
            csi=(chatServerInterface)Naming.lookup(arg);
            csi.joinChatRoom(name,(chatClientInterface)this);
            this.chatStart();
            }catch(Exception ex)
            {
                //JOptionPane.showMessageDialog(null, "Failed\n"+ex.printStackTrace());
                //System.out.println("Failed to bind\n"+ex.printStackTrace());
                ex.printStackTrace();
                System.exit(0);
            }


    }

When you cast null with a type, it doesn't really convert it to an object with null value. As a result, it is not the type object that is sent but is still null. Then at the server side, it doesn't know what was coming in (argument). I guess you didn't handle this null value on your server side either? How your server side implement for this part? Even though you may handle this issue in your client side before you attempt to join, it would be better to handle it on your server side.

This is my server

public synchronized void joinChatRoom (String clientName, chatClientInterface c) throws RemoteException
    {

                    if (clientList.containsKey(clientName))
                    {
                       c.nameAvailable();
                    }
                    else
                    {
                        System.out.println(clientName+" is now joined the chat room!");
                        clientList.put(clientName,c);

                        Collection cc= clientList.keySet();
                        Iterator itt= cc.iterator();

                        while(itt.hasNext())
                        {
                            chatClientInterface client=(chatClientInterface)itt.next();
                            try{
                                client.receiveMessage(clientName+" is now joind the chat room!", clientName);
                            }catch(Exception ex)
                            {
                                System.out.println("failed to join "+clientName+ "into chat room");
                                ex.printStackTrace();
                            }

                    }

                    }


    }

Do i need to remove null?

chatServerInterface csi;

Oh sorry, what I mean is this line...

csi=(chatServerInterface)Naming.lookup(arg);

What happen to csi after that line? Did you check it up before you attempt to join the server chat room?

This is the implementation.

public ClientImplementation(String name) throws RemoteException
    {
            String arg = "rmi://localhost:2828/chatfun";
            myName=name;

            try{
            csi=(chatServerInterface)Naming.lookup(arg);
            csi.joinChatRoom(name,(chatClientInterface)this);
            this.chatStart();
            }catch(Exception ex)
            {

                ex.printStackTrace();
                System.exit(0);
            }
    }
     public void chatStart()
    {
    Scanner in = new Scanner(System.in);
        while (true) {
            try {
                String lastMessage = in.nextLine();
                csi.sendToAll(lastMessage,myName);
            }catch (Exception e) {
                System.out.println("exiting with error...");
                System.err.println(e);
                System.exit(1); //exit
            }
        }
    }

What do you mean by check csi? You mean check for null.
I add this line

if(csi!=null)
   csi.joinChatRoom(name,this);

But still gives me the exception.

I cast this because it gives me a IllegalArgumentException, I thought to cast "this".

(chatClientInterface)this

PLZ help me solve this.

What is not the implementation of what? That post makes little sense.
Have you tried printing csi immediately after the Naming.lookup? You say it's null, but the API documentation says lookup will throw various Execptions rather than just return null if there is a problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.