• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer cannot be cast to client

 
Wes Traylor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking for some help on a Client Server program for class. I am stuck on an error that says "java.lang.Integer cannot be cast to client1.Client$Employee". Not for sure what this is and have spent several hours working on it and trying different things. Anyone have any ideas?

Server Code:



Client Code:




 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Wes,

That's a lot of code; it would sure help if you'd give us all the information the compiler is giving you -- i.e., the line number where the error occurs!

But in general terms, it looks like you're using serialization to send objects between a client and server process over a socket. I see Strings, Integers, and Employee objects being written, and I see places in the code where you're reading an object and expecting it to be an Integer, a String, or an Employee, at different points. In any case, the error just means -- and I think you probably know this -- that at one of the points where you're expecting an Integer, the object you're getting over the socket is actually an Employee. All I can tell you is that you need to go over the logic, carefully, and make sure you understand the conditions under which each kind of object can be transmitted; and make sure that the other end always has a way to know what's coming next.
 
Wes Traylor
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get this error from the code when I try to use the FindListener:

java.lang.Integer cannot be cast to client1.Client$Employee

On the client side this is the part of the code I'm having trouble with.

Sorry for the long code, wanted to include everything so that someone could copy and paste to run.


 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The server logs some messages for every request it processes. When you run this, and have an error with the FIND command, do you see FIND-related messaages on the server? What do they say?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that when your Server first connects to a client, it sends an Integer as a handshake, and your Client never reads that Integer. So later, when you do a FIND, then read the input stream you get the Integer that has been sitting there in the buffer waiting to be taken.

You should read the integer immediately after making the connection to the Server:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic