Skip to content
Advertisement

java.lang.ClassCastException exception using ObjectInputStream, when sending a class as an object

https://github.com/IshayKom/RCProject

Classes As Shown in the eclipse package manager

(I don’t really deal with github so I don’t know how to upload classes correctly)

I got an error running my project.

I use VMWare to work on my project but I don’t think that this specific error requires the use of multiple PCs or VMs.

It basically should receive ClientInformation from the InitiHandler class and start the process of matching two clients together. (Security isn’t required in this project at the moment)

The steps to recreate this issue as follows: Enabling the “server” with the required information. After that go on “controlled client” mode, write the required information, and attempt to send info to the server.

I tried searching for a solution and looking at what mistake I did this time but I just can’t get my mind into it. If anyone can spot my mistake it’ll super helpful.

The following is the class which the error happened in:

    package Server;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerInitializer extends Thread {
    private int port;
    private ClientInformation[] ci = null;
    private ClientInformation c = null;
    private boolean run = true;
    ServerSocket serversocket;
    ObjectInputStream ois;
    Socket client;
    public ServerInitializer(int port, int clientlimit) {
        this.port = port;
        ci = new ClientInformation[clientlimit];
        start();
    }

    
    @Override
    public void run() {
        try
        {
            serversocket = new ServerSocket(port);
            while(run)
            {
                client = serversocket.accept();
                System.out.println("New Client Has Been Connected, ip:" + client.getInetAddress().getHostAddress());
                
                ois = new ObjectInputStream(client.getInputStream());
                c = (ClientInformation) ois.readObject();
                
                new ServerThread(ci,c, client, run);
            }
                serversocket.close();
        }
        catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public void terminate()
    {
        this.run = false;
    }
    public boolean getrun()
    {
        return run;
    }
}

The error itself: “Exception in thread “Thread-0″ java.lang.ClassCastException: class Startup.ClientInformation cannot be cast to class Server.ClientInformation (Startup.ClientInformation and Server.ClientInformation are in unnamed module of loader ‘app’) at Server.ServerInitializer.run(ServerInitializer.java:35)”

If it’s too much of a pain to see my mistake based on what I currently wrote let me know what to do to make it easier for you to spot it.

Another Question: How can I use the terminate function to basically disable any while loops that are happening in other classes/threads? or what I wrote is good enough? I couldn’t test this because of my error. (This can apply to the server itself in my project or the client-side)

Advertisement

Answer

You have a class named ClientConfiguration, which has package Server; on the top. You have a completely different, totally unrelated class which by pure coincidence is also named ClientConfiguration, but has package Startup; at the top, which is why I say it is unrelated. Because that’s what that means. You are then sending an instance of one of these, and upon receiving it, you assume it is the other. It isn’t, hence, CCEx.

If you intended to have 2 different classes with the same name, stop confusing them; if this sounds difficult (I think it’d have some issues with this, too!), then rename one.

If you never intended to have 2 separate classes, then fix that problem. Possibly you already did and simply replaced the package statement (and moved the source file to another folder) for the only ClientConfiguration you ever had, but then one of the two (client or server) is running old code.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement