2

I tried to send an object from my android phone to my computer, but I keep getting this error that it cant recognize the class of this object. Thats strange because, when I tried to send an ocject from the computer to itself it worked using the same way. Both of the packages have the User class, and it implements Serializable. please tell me why its happening.

computer (server):

private ServerSocket server;
private ObjectInputStream input;
private Socket connection;
private User message;

public void brains() throws IOException {
    while (true) {
        System.out.println("waiting for connection...");
        try {
            server = new ServerSocket(4567, 100);
            connection = server.accept();
            System.out.println("you are now conected");
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        System.out.println("Setting up Streams..");

        input = new ObjectInputStream(connection.getInputStream());
        try {
            message = (User) input.readObject();
        } catch (ClassNotFoundException ex) {
            System.out.println("I dont know wtf");
        }

        System.out.print("the user ");
        message.printUserName();
        System.out.print(", used your app at ");
        message.printTime();
        System.out.println("");
                    System.out.println("Shutting down!");
        try {
            input.close();
            connection.close();
            server.close();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

    }

}

phone (client):

private User user;
private Socket connection;
private ObjectOutputStream output;

public void run() {

    try{
    connection = new Socket (InetAddress.getByName(ip), 4567);
    output = new ObjectOutputStream(connection.getOutputStream()) ;

    user = new User("bla bla", "14:09");
    output.writeObject(user);
    output.flush();



    }catch(Exception e){
        System.out.print(e);
    }finally{
        try{
        output.close();
        connection.close();
        }
    catch(Exception e){
        System.out.print(e);
    }




    }



}

this is the console log :

waiting for connection...
you are now conected
Setting up Streams..
Dont recognize the class
the user Exception in thread "main" java.lang.NullPointerException
at whosUsing.whosUsing.brains(whosUsing.java:34)
at whosUsing.test.main(test.java:9)
  • do both User classes exist in the exact same package? see http://stackoverflow.com/questions/2916107/readobject-method-throws-classnotfoundexception – coderatchet Nov 10 '13 at 23:51

2 Answers2

0

If a class is not in exactly the same package in both the client and the server, then the class that comes through will not be recognised by the system.

put the User.java file in the same package: "com.myapp.shared.User". this should solve your problem.

see readobject method throws ClassNotFoundException for a similar issue.

Community
  • 1
  • 1
coderatchet
  • 8,120
  • 17
  • 69
  • 125
0

Be sure that the package name and classname of the client on phone must be exactly the same of server side on computer . Take a look on this. Hope it help

ObjectInputStream readObject(): ClassNotFoundException

Community
  • 1
  • 1
TheATeam
  • 26
  • 2