This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Integer cannot be cast to client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Integer cannot be cast to client" Watch "Integer cannot be cast to client" New topic
Author

Integer cannot be cast to client

Wes Traylor
Greenhorn

Joined: Apr 05, 2009
Posts: 3
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Wes Traylor
Greenhorn

Joined: Apr 05, 2009
Posts: 3
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Jan 28, 2003
Posts: 4165
    
  21

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:


Steve
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Integer cannot be cast to client
 
Similar Threads
subclasses throw null exception when using objectoutput stream
Need Client Server Help!
Help Client Server implementaion
Can't write collection back to disk
Client server help