4

My project gets class Cast Exception when I started server and then I tried to add customer using add customer UI but when I tried for it it returns a Class Cast Exception from the server connector class.

interface CustomerController

    public interface CustomerController {
    public boolean addCustomer(Customer customer)throws RemoteException,IOException,ClassNotFoundException;
}

ServerStart,java

    public class ServerStart {
    public static void main(String[] args) {
          try {
            Registry registry=LocateRegistry.createRegistry(5050);
            System.out.println("Server is starting..");
            registry.rebind("Server", new CustomerControllerImpl());
        } catch (RemoteException ex) {
            Logger.getLogger(ServerStart.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

ServerConnector.java

    public class ServerConnector {

    private static ServerConnector serverConnector;
    private CustomerController customerController;

    private ServerConnector() throws NotBoundException, MalformedURLException, RemoteException {
        customerController = (CustomerController) Naming.lookup("rmi://localhost:5050/Server");
    }

    public static ServerConnector getServerConnector() throws NotBoundException, MalformedURLException, RemoteException {
        if (serverConnector == null) {
            serverConnector = new ServerConnector();
        }
        return serverConnector;
    }

    public CustomerController getCustomerController() {
        return customerController;
    }
}

Class cast Exception occurs at ServerConnector.java file at

customerController = (CustomerController) Naming.lookup("rmi://localhost:5050/Server");

CustomerControllerImpl.java

    public class CustomerControllerImpl extends UnicastRemoteObject implements CustomerController{

    private final CustomerFileAccess customerFileAccess = new CustomerFileAccess();

    public CustomerControllerImpl() throws RemoteException{

    }

    @Override
    public boolean addCustomer(Customer customer) throws RemoteException, IOException, ClassNotFoundException {
        return customerFileAccess.addCustomer(customer);
    }
}

here I attached the netbeans project which can be download thourgh this link

Thank you!.

8
  • 2
    Post any code related to the question here not on an external site Commented Sep 8, 2015 at 13:32
  • Could you please add the code where the actual ClassCastException happens? And am I right to assume the exception happens on the client side? Commented Sep 8, 2015 at 13:33
  • @LarsGendner customerController = (CustomerController) Naming.lookup("rmi://localhost:5050/Server"); Commented Sep 8, 2015 at 13:42
  • First question: Are you sure CustomerControllerImpl has been declared as implementing CustomerController? (Incidentally, a ClassCastException will normally have a message saying something along the lines of "X cannot be cast to Y" which can often help diagnose the issue.) Commented Sep 8, 2015 at 13:52
  • @daiscog yes I have implemented CustomerController for class CustomerControllerImpl I have also added that code part at the end of the question Commented Sep 8, 2015 at 14:02

1 Answer 1

4

Looking through the docs, I believe it may be because your interface does not extend java.rmi.Remote.

Sign up to request clarification or add additional context in comments.

1 Comment

No worries. If you're ever in Cardiff, buy me a beer ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.