Remote Exception Error
843793Mar 18 2007 — edited Mar 18 2007I have a simple chat program running and it all works fine until i try to log out a user then i receive a
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: Error.
Here is my code:
Client:
import java.awt.*;
import java.awt.event.*;
import java.rmi.*;
import java.rmi.server.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
public class Client extends JFrame
implements ActionListener, Notifiable
{
JTextField chatField, userlog;
JTextArea chatWindow;
static JTextArea userList;
Label currentBidLabel, productLabel;
JButton Chat, Exit, Clear, userButton, History;
JOptionPane optionPane;
private String currentList = " ";
private static String username = "";
private String currentChat = "";
private String msg = "";
String users = "";
static ClientInterface Server;
public Client() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
JPanel jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setPreferredSize(new java.awt.Dimension(392, 134));
{
JPanel viewPanel = new JPanel();
jPanel1.add(viewPanel);
viewPanel.setPreferredSize(new java.awt.Dimension(390, 198));
{
JScrollPane jScrollPane1 = new JScrollPane();
viewPanel.add(jScrollPane1);
jScrollPane1.setPreferredSize(new java.awt.Dimension(242, 192));
{
chatWindow = new JTextArea();
jScrollPane1.setViewportView(chatWindow);
chatWindow.setEnabled(false);
}
}
{
userList = new JTextArea();
viewPanel.add(userList);
userList.setText("");
userList.setPreferredSize(new java.awt.Dimension(127, 195));
userList.setEnabled(false);
}
}
{
JPanel buttonPanel = new JPanel();
jPanel1.add(buttonPanel);
buttonPanel.setPreferredSize(new java.awt.Dimension(390, 33));
{
Chat = new JButton();
buttonPanel.add(Chat);
Chat.setText("Enter");
Chat.setPreferredSize(new java.awt.Dimension(90, 22));
Chat.addActionListener(this);
Chat.setEnabled(false);
}
{
History = new JButton();
buttonPanel.add(History);
History.setText("History");
History.setPreferredSize(new java.awt.Dimension(90, 22));
History.addActionListener(this);
History.setEnabled(false);
}
{
Clear = new JButton();
buttonPanel.add(Clear);
Clear.setText("Clear");
Clear.setPreferredSize(new java.awt.Dimension(90, 22));
Clear.addActionListener(this);
Clear.setEnabled(false);
}
{
Exit = new JButton();
buttonPanel.add(Exit);
Exit.setText("Close");
Exit.setPreferredSize(new java.awt.Dimension(90, 22));
Exit.addActionListener(this);
}
}
{
chatField = new JTextField();
jPanel1.add(chatField);
chatField.setText("");
chatField.setPreferredSize(new java.awt.Dimension(385, 29));
chatField.setEnabled(false);
}
{
JPanel jPanel2 = new JPanel();
jPanel1.add(jPanel2);
jPanel2.setPreferredSize(new java.awt.Dimension(382, 40));
{
userlog = new JTextField();
jPanel2.add(userlog);
userlog.setText("Enter Username");
userlog
.setPreferredSize(new java.awt.Dimension(236, 27));
}
{
userButton = new JButton();
jPanel2.add(userButton);
userButton.setText("Enter Username");
userButton.setPreferredSize(new java.awt.Dimension(117, 22));
userButton.addActionListener(this);
}
}
}
getRootPane().setDefaultButton(userButton);
pack();
this.setSize(400, 351);
setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
try {
UnicastRemoteObject.exportObject(this);
}
catch(RemoteException re) {
re.printStackTrace();
}
}
public void actionPerformed(ActionEvent ae) {
Component listener = (Component)ae.getSource();
if(listener == this.userButton){
username = this.userlog.getText();
try {
System.out.println("check user 1" + username);
Server.addUsername(username);
msg = (username + " has just logged in");
Server.setChat(msg);
System.out.println("check user 2");
//userList.setText("");
Server.getUserName();
Chat.setEnabled(true);
Clear.setEnabled(true);
History.setEnabled(true);
chatField.setEnabled(true);
userButton.setEnabled(false);
userlog.setEnabled(false);
getRootPane().setDefaultButton(Chat);
}
catch (RemoteException re) {
re.printStackTrace();
}
System.out.println("check");
chatField.setText("");
userlog.setText("");
}
else if(listener == this.Chat){
msg = this.chatField.getText();
msg = (username + " says " + msg);
try {
Server.setChat(msg);
chatField.setText("");
}
catch (RemoteException re) {
re.printStackTrace();
}
System.out.println("check");
}
else if(listener == this.History){
try {
chatWindow.setText("");
chatWindow.setText(Server.getHistory());
}
catch (RemoteException re) {
re.printStackTrace();
}
/* chatWindow.setText("");
chatWindow.setText(msg);*/
}
else if(listener == this.Clear){
chatWindow.setText("");
}
else if(listener == this.Exit){
/*try {
Server.logout(username);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
dispose();
System.exit(0);
}
}
public void notify(Integer reason) {
if(reason.intValue() == 0) {
try {
currentList = Server.getUserName();
//userList.setText("");
userList.setText(currentList);
currentChat = Server.getChat();
chatWindow.append(currentChat + "\n");
}
catch (RemoteException re) {
re.printStackTrace();
}
}
}
public void notify2(Integer reason) {
if(reason.intValue() == 0) {
try {
currentList = Server.getUserName();
userList.setText(currentList);
/*currentChat = auctioneer.getCurrentBid();
chatWindow.append(currentChat + "\n");*/
}
catch (RemoteException re) {
re.printStackTrace();
}
}
}
public static void main(String [] args) {
String serverName = "rmi://localhost/chat";
Client chat_client = new Client();
try {
chat_client.Server = (ClientInterface) Naming.lookup(serverName);
chat_client.Server.registerForNotification(chat_client);
}
catch (NotBoundException nbe) {
nbe.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Server.java
import java.rmi.*;
import java.rmi.server.*;
import java.net.*;
import java.util.*;
public class Server extends UnicastRemoteObject
implements ClientInterface
{
private Vector chatConvo;
private Vector usersOnline;
private String list = "";
Vector clientList = new Vector();
//ArrayList<String> ClientNicknames = null;
public Server()
throws RemoteException
{
super();
/* setProduct("rtrd");
setMinBid("345");*/
// ClientNicknames = new ArrayList();
chatConvo = new Vector();
usersOnline = new Vector();
chatConvo.addElement("");
}
public String getChat() throws RemoteException {
//chatWindow.append(msg + "\n");
return (String) chatConvo.lastElement();
}
public String getUserName() throws RemoteException {
System.out.println("check user 4");
list = "";
for(Enumeration users = usersOnline.elements();
users.hasMoreElements();) {
list = (list + (String) users.nextElement() + "\n");
/*Notifiable thingToNotify = (Notifiable) users.nextElement();
thingToNotify.notify(new Integer(0));*/
}
System.out.println(list);
return list;
}
public String getHistory() throws RemoteException {
System.out.println("check user 4");
list = "";
for(Enumeration chat = chatConvo.elements();
chat.hasMoreElements();) {
list = (list + (String) chat.nextElement() + "\n");
}
//list = "checkin this shit";
System.out.println(list);
return list;
}
public void addUsername( String username) throws RemoteException
{
System.out.println("check user 3");
usersOnline.addElement(username);
for(Enumeration clients = clientList.elements();
clients.hasMoreElements();) {
Notifiable thingToNotify = (Notifiable) clients.nextElement();
thingToNotify.notify(new Integer(0));
}
System.out.println("checknick");
}
public void logout( String username) throws RemoteException
{
System.out.println("logout :" + username);
//usersOnline.addElement(username);
/*for(Enumeration users = usersOnline.elements();
users.hasMoreElements();) {*/
//System.out.println(users.nextElement());
//if (usersOnline.contains(username)){
usersOnline.remove(username);
// }
//}
for(Enumeration clients = clientList.elements();
clients.hasMoreElements();) {
Notifiable thingToNotify = (Notifiable) clients.nextElement();
thingToNotify.notify(new Integer(0));
}
}
public void setChat(String b) throws RemoteException {
chatConvo.addElement(b);
for(Enumeration clients = clientList.elements();
clients.hasMoreElements();) {
Notifiable thingToNotify = (Notifiable) clients.nextElement();
thingToNotify.notify(new Integer(0));
}
}
public void registerForNotification(Notifiable n) throws RemoteException {
//thingToNotify = n;
clientList.addElement(n);
}
public static void main(String [] args) {
String name = null;
try {
name = "rmi://localhost/chat";
} catch (Exception ue) {}
//System.setSecurityManager(new RMISecurityManager());
try {
Server thisOne = new Server();
Naming.rebind(name, thisOne);
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("Bind..");
}
}
ClientInterface:
import java.rmi.*;
interface ClientInterface extends Remote {
public void setChat(String chat) throws RemoteException;
public String getChat() throws RemoteException;
public String getHistory() throws RemoteException;
public String getUserName() throws RemoteException;
public void addUsername(String username) throws RemoteException;
public void logout(String username) throws RemoteException;
//public String getProduct() throws RemoteException;
// called by clients to register for server callbacks
public void registerForNotification(Notifiable n) throws RemoteException;
}
Notifiable:
import java.rmi.*;
public interface Notifiable extends Remote {
public void notify(Integer reason) throws RemoteException;;
}