There is a client (Login
) that sends an object (objRequest
) to the server via the connection class (clentCon
). The server (ServerWindow
) receives an object (each client creates its own echoThread
), works with it, collects a response object (objResponse
) and sends it to the client … There are 3 variables in the objResponse
class (ResultSet
, int
, String
, all have default values, constructors just assign new ones values for two of them), the implements Serializable
class.
The client is currently accessing the server in two places. In one (“test connection”) in the returned object, the int
and String
are changed and everything works fine. In the second (implies a query to the database) ResultSet
and String
should change, but I get the following:
java.io.NotSerializableException: com.mysql.jdbc.JDBC4ResultSet
at java.io.ObjectOutputStream.writeObject0 (Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields (Unknown Source)
at java.io.ObjectOutputStream.writeSerialData (Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject (Unknown Source)
at java.io.ObjectOutputStream.writeObject0 (Unknown Source)
at java.io.ObjectOutputStream.writeObject (Unknown Source)
at logic.echoThread.run (echoThread.java:60)
server side and
java.lang.NullPointerException
at keyringWindows.Login.checkPassword (Login.java:257)
at keyringWindows.Login $ 3.actionPerformed (Login.java:151)
at javax.swing.AbstractButton.fireActionPerformed (Unknown Source)
at javax.swing.AbstractButton $ Handler.actionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
at javax.swing.DefaultButtonModel.setPressed (Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased (Unknown Source)
at java.awt.Component.processMouseEvent (Unknown Source)
at javax.swing.JComponent.processMouseEvent (Unknown Source)
at java.awt.Component.processEvent (Unknown Source)
at java.awt.Container.processEvent (Unknown Source)
at java.awt.Component.dispatchEventImpl (Unknown Source)
at java.awt.Container.dispatchEventImpl (Unknown Source)
at java.awt.Component.dispatchEvent (Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent (Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent (Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent (Unknown Source)
at java.awt.Container.dispatchEventImpl (Unknown Source)
at java.awt.Window.dispatchEventImpl (Unknown Source)
at java.awt.Component.dispatchEvent (Unknown Source)
at java.awt.EventQueue.dispatchEventImpl (Unknown Source)
at java.awt.EventQueue.access $ 200 (Unknown Source)
at java.awt.EventQueue $ 3.run (Unknown Source)
at java.awt.EventQueue $ 3.run (Unknown Source)
at java.security.AccessController.doPrivileged (Native Method)
at java.security.ProtectionDomain $ 1.doIntersectionPrivilege (Unknown Source)
at java.security.ProtectionDomain $ 1.doIntersectionPrivilege (Unknown Source)
at java.awt.EventQueue $ 4.run (Unknown Source)
at java.awt.EventQueue $ 4.run (Unknown Source)
at java.security.AccessController.doPrivileged (Native Method)
at java.security.ProtectionDomain $ 1.doIntersectionPrivilege (Unknown Source)
at java.awt.EventQueue.dispatchEvent (Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters (Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter (Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy (Unknown Source)
at java.awt.EventDispatchThread.pumpEvents (Unknown Source)
at java.awt.EventDispatchThread.pumpEvents (Unknown Source)
at java.awt.EventDispatchThread.run (Unknown Source)
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.mysql.jdbc.JDBC4ResultSet
at java.io.ObjectInputStream.readObject0 (Unknown Source)
at java.io.ObjectInputStream.defaultReadFields (Unknown Source)
at java.io.ObjectInputStream.readSerialData (Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject (Unknown Source)
at java.io.ObjectInputStream.readObject0 (Unknown Source)
at java.io.ObjectInputStream.readObject (Unknown Source)
at logic.clientCon.run (clientCon.java:36)
Caused by: java.io.NotSerializableException: com.mysql.jdbc.JDBC4ResultSet
at java.io.ObjectOutputStream.writeObject0 (Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields (Unknown Source)
at java.io.ObjectOutputStream.writeSerialData (Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject (Unknown Source)
at java.io.ObjectOutputStream.writeObject0 (Unknown Source)
at java.io.ObjectOutputStream.writeObject (Unknown Source)
at logic.echoThread.run (echoThread.java:60)
from the client side.
As far as I can tell, the problem is somewhere in echoThread
public class echoThread extends Thread
{
protected Socket soc = null;
private ObjectInputStream objins = null;
private ObjectOutputStream objouts = null;
private Connection con = null;
public JTextPane tp;
public echoThread (Socket soc, Connection con, JTextPane tp)
{
this.soc = soc;
this.con = con;
this.tp = tp;
}
public void run ()
{
try
{
objins = new ObjectInputStream (soc.getInputStream ());
objouts = new ObjectOutputStream (soc.getOutputStream ());
while (true)
{
try
{
objRequest request = (objRequest) objins.readObject ();
sleep (250);
String type = request.getType ();
tp.setText (tp.getText () + String.format ("Received a request of type:% s \ n", type));
objResponse resp = null;
if (type.equals ("select"))
{
Statement st = con.createStatement ();
resp = new objResponse (st.executeQuery (request.getQuery ()), type);
st.close ();
}
else if (type.equals ("check"))
{
resp = new objResponse (123, "checked");
}
else
{
PreparedStatement statement = con.prepareStatement (request.getQuery ());
resp = new objResponse (statement.executeUpdate (), type);
statement.close ();
}
objouts.writeObject (resp);
objouts.flush ();
}
catch (Exception ex)
{
ex.printStackTrace ();
return;
}
}
}
catch (Exception ex)
{
JOptionPane.showMessageDialog (null, "Connection error. \ N" + ex.getMessage ());
}
finally
{
try
{
tp.setText (tp.getText () + "Client disconnected \ n");
soc.close ();
objins.close ();
objouts.close ();
}
catch (Exception ex)
{
JOptionPane.showMessageDialog (null, "Connection error. Server application will shutdown.");
System.exit (0);
}
}
}
}
Please help me figure out what’s going on.
Answer 1, authority 100%
The problem is that the ResultSet
that you are trying to shove into the ObjectOutputStream
within the objResponse
object is not serializable, so it cannot be (sorry for tautology) to serialize, that is, to write to ObjectOutputStream
. Hence the problem on the client: since the server was unable to write data to the response, a NullPointerException
got out on the client side. Good article on Java serialization .
You should reconsider the communication protocol between the client and the server. Why is the server serving the raw ResultSet
? It would be better to pull the necessary data out of it on the server, add it to some serializable object, and only then transfer it to the client.
Also: close the statement
in the finally
block. If an error occurs in your code while executing an SQL query, the statement
will remain unclosed and you will have a memory leak.
Update
Still, it is better to consider the option of providing the result in the form of an object of a specific class. In general, unless you are writing an application whose goal is to allow the user to execute any SQL queries on the server, it’s better if the client doesn’t know how and where the data is coming from. If you hate to write a separate class for each request, use List & lt; Map & lt; String, Object & gt; & gt;
, not ResultSet
.