ClassCastException with readObject

L

lei

Hello,

I'm having trouble of getting ClassCastException in my code:
I'm trying to get file from server side. In my servlet service, I get
retObjct, it will pass to client side.

....
if (servicetype.equals( RCComm.GET_FILE ))
{
retObject = (Object) getFile( params );
EHEADER testob = null;
try{
testob = (EHEADER)retObject;
}
catch(Exception ex)
{//not come in here, since there's no ClassCastException
printToLogFile( "*****In service in
Servlet.java,ex.getMessage()=="+ex.getMessage());
printToLogFile( "*****In service in Servlet.java,testob=="+testob);
}
}
..... return retObject;


for above servlet code, there's no exception, which means the retObject
can be Cast as EHEADER.
But, after I pass it to client side.
In my client side, I use getObject to get the result.
ClassCastException happens here. Please check
below. I need to cast the received object to EHEADER for further
process. It seems the object
changed after server sending it and before client receiving it? Please
let me know why this happen and how can I fix it?

//===============================================================================
private static Object getObject( Hashtable params )
throws Exception
//===============================================================================
{
Object retObject = null;

try
{
.....
ObjectInputStream in = new ObjectInputStream( con.getInputStream() );
retObject = (Object) in.readObject();

EHEADER testobj = null;
try{
testobj = (EHEADER)retObject;
}
catch(Exception ex)
{
System.out.println( ex.getMessage() );//ClassCastException here,
testobj is null.
ex.printStackTrace();

}
.....
}
catch (IOException ioe)
{
ioe.printStackTrace();
throw new Exception( ioe.getMessage() );
}
 
R

robson

Try adding serial version uid field to EHEADER class.

private static final long serialVersionUID = -7147010951202938127L;

- value can be automatically generated by Eclipse.I assume EHEADER
iimplements Serializable?
 
L

lei

Thanks for your reply.
THe EHEADER.java already has serialVersionUID.
and it implements Serializable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top