ClassNotFoundException in an object conversion
I've been reading the forums for a solution to this problem, but I haven't been able to - be gentle. :P
I'm trying to implement my own networking package that mimics the SQL API already implemented. I know you're asking why, so, the reason is, I'm developing software for a handheld that needs more fault tolerance than the package provides.
Anyway, so, basically, I'm using buffered streams over a socket to transmit an SQL request as a string. Then I transmit back the results in my own Results class (No need to worry about how it's implemented, that works fine). The Results class implements Serializable.
I use this static method to rebuild my objects after transmission:
publicstatic Object byteArrayToObject(byte[] data)throws IOException, ClassNotFoundException
{
ByteArrayInputStream bis =new ByteArrayInputStream(data);
ObjectInputStream ois =new ObjectInputStream(bis);
Object temp = ois.readObject();
ois.close();
bis.close();
return temp;
}
The strange thing about this is, I have only three types of actions my NetSQLClient class can take. NetSQLClient.executeQuery(String), NetSQLClient.executeUpdate(String) and NetSQLClient.execute(String). They all behave similary to the equivalent methods from Statement.
In all other places where I execute NetSQLClient.executeQuery(String), it works fine. I can query a user table for authentication, I can query my field information to build the UI, etc. But in one place, I get a ClassNotFoundException on a query. Here's the process:
Private class within the Client class:
privateclass SocketReaderimplements Runnable
{
BufferedInputStream in;
public SocketReader(InputStream input)
{
in =new BufferedInputStream(input);
}
publicvoid run()
{
while(running)
{
try
{
byte[] size =newbyte[4];
in.read(size);
int incomingSize = Convert.byteArrayToInt(size);
if(incomingSize == 0)
{
disconnect();
}
else
{
byte[] data =newbyte[incomingSize];
in.read(data);
if(listener !=null)
{
byte[] trans =newbyte[data.length];
System.arraycopy(data, 0, trans, 0, trans.length);
listener.dataRecieved(trans);
}
}
}
catch(IOException e)
{
running =false;
break;
}
}
}
}
The above is a private class that is a simple recieve thread. The listener is an interface I made so data can be transmitted out of the Client class. It goes here, in the NetSQLClient class:
publicsynchronizedvoid dataRecieved(byte[] data)
{
this.data = data;
notify();
}
The part of NetSQLClient that is waiting:
publicsynchronized Results executeQuery(String query)throws NetSQLException
{
try
{
client.write(101);
client.write(query);
wait();
return (Results)Convert.byteArrayToObject(data);
}
catch(InterruptedException e)
{}
catch(IOException e)
{
thrownew NetSQLException("Internal I/O error.");
}
catch(ClassNotFoundException e)
{
thrownew NetSQLException("Internal class error.");
}
catch(ClassCastException e)
{
try
{
throw (NetSQLException)Convert.byteArrayToObject(data);
}
catch(IOException ef)
{
thrownew NetSQLException("Internal IO error type 2.");
}
catch(ClassNotFoundException ef)
{
thrownew NetSQLException("Internal class error type 2.");
}
catch(ClassCastException ef)
{
thrownew NetSQLException("Internal casting error.");
}
}
returnnull;
}
As you can see, this is where I call Convert.byteArrayToObject(byte[] data). And this is where the exception is generated in only one circumstance. Here's is the code that calls it, although I dunno how it's different from any other place I call that method:
Results rs = client.executeQuery("SELECT * FROM serial_asset WHERE asset_number = '" + serial +"';");
Anyway, any help would be appreciated. I have no idea what's going on anymore.

