Hi everyone,
I'm doing a RMI chat application for my client-server distributing programming module. I have stuck nearly at the very end of it. What I doing is: running the server. running the the UI, connecting to the server. Loging as user1. sending the priv messagae to other user. Replaying back. And all of that works fine till I get to the point when I want to send again a priv message to user that is not logged in. i can send a message, i can login in as this user, i do get the message but problem occurs when I want text back. Somehow I'm getting IndexOutOfBoundException. Can some one give me an advice. Thank you.

The exception is pointing on this piece of code:

public void PrintPrivMSG(Message receivedMessage) {

      //geting the message from the server and putting different details into separate variables
      System.out.print(frames.size());
      String senderID = receivedMessage.getSenderID();
      String message = receivedMessage.getMessage();
      String date = receivedMessage.getDate();
      String userName = clients.get(Integer.valueOf(senderID) - 1).getName();             <---- Exception points at this line
      String printMessage = userName + ":\t\t" + date + "\n          " + message + "\n";
      //creating private message interface
      PrivateMsgInterface currentFrame;
      boolean isSend = false;
      //checking if the array list of frames is empty 
      if (!frames.isEmpty()) {
          for (int i = 0; i < frames.size() -1; i++) {
              currentFrame = frames.get(i);
              //checking if the frame is already opend if yes setting its visability to true
              if (currentFrame.id == Integer.valueOf(senderID)) {
                  currentFrame = frames.get(i);
                  currentFrame.printMsg(printMessage);
                  isSend = true;

                  frames.get(i).setVisible();
              }
          }
      }

Excpetion:

java.lang.IndexOutOfBoundsException: Index: 13, Size: 12
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at GUIHandling.ClientInterface.PrintPrivMSG(ClientInterface.java:292)
    at ClientRMI.ClientCallbackImpl.sendPrivateMessage(ClientCallbackImpl.java:42)
    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 java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
    at $Proxy1.sendPrivateMessage(Unknown Source)
    at ServerRMI.ServerImpl.sendPrivMsg(ServerImpl.java:364)
    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)

All anyone can tell from what you've posted is that you try to access element 13 in an ArrayList (client) that has 12 elements. The reason for that lies somewhere in all the other code that we haven't seen.
First, decide whether the index is wrong, or the client list is wrong. Then use print statemnents to trace whichever it is back to the point where it goes wrong.

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.