9 Replies - 1108 Views - Last Post: 11 April 2011 - 08:59 AM Rate Topic: -----

#1 xtr3mnico  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 89
  • Joined: 26-February 10

List & Socket Problem, io.NotSerializable Error

Posted 31 March 2011 - 10:00 PM

My Object is to get a list of products from the server, and write it back through the socket to the client where i can safely copy it to another LinkedList and use it as i see fit.
This is a snippet of my client server problem the follow is on the server program.

Product Class

public class Products{
    String pCode, pName, pShDesc,pLoDesc,supplier;
    int itemsInStock,low;
    double salePrice,unitPrice;

    public Products(){
        this.pCode="";
        this.pName="";
        this.pShDesc="";
        this.pLoDesc="";
        this.itemsInStock=0;
        this.low=50;
        this.salePrice=0.0;
        this.unitPrice=0.0;
        this.supplier="";
    }


Client Class has objects along with others that are not used witin this problem all are initialized properly and been tested over the server

protected Socket socket = null;
    protected ObjectInputStream in = null;
    protected ObjectOutputStream out = null;


Now the client initiates a call to server via
        List<Products> sList = (LinkedList) client.in.readObject();


The server now responds to the call by doing:
out.writeObject(selectProduct());


This calls the select Product function:
 public List<Products> selectProduct()
    {
            Products obj = null;
            List<Products> sList=new LinkedList<Products>();

            try
            {
                    stat = con.createStatement();
                    String query = "SELECT * FROM Product";
                    res = stat.executeQuery(query);
                    while(res.next())
                    {
                            obj = new Products();
                            obj.setpCode(res.getString(1));
                            obj.setpName(res.getString(2));
                            obj.setpShDesc(res.getString(3));
                            obj.setpLoDesc(res.getString(4));
                            obj.setItemsInStock(res.getInt(5));
                            obj.setLow(res.getInt(6));
                            obj.setSalePrice(res.getDouble(7));
                            obj.setUnitPrice(res.getDouble(8));
                            obj.setSupplier(res.getString(9));
                            sList.add(obj);
                    }

            }
            catch(SQLException ex)
            {
                System.out.println("Error occured\n" + ex.getMessage()) ;
            }
            return sList;
    }


The problem occurs when the server is writing to the client via
out.writeObject(selectProduct());


Where the out.writeObject throws the error on the client side
IOException occurred
writing aborted; java.io.NotSerializableException: object.Products

server side error
IO Exception
object.Products
[Ljava.lang.StackTraceElement;@8965fb

Ive been reading and looking around for hours... any idea how i can fix this? Tried to be as detailed as i could.

Is This A Good Question/Topic? 0
  • +

Replies To: List & Socket Problem, io.NotSerializable Error

#2 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: List & Socket Problem, io.NotSerializable Error

Posted 31 March 2011 - 11:21 PM

Your "Products" class does not implement the serialisable interface.

This post has been edited by masijade: 31 March 2011 - 11:22 PM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,860
  • Joined: 27-December 08

Re: List & Socket Problem, io.NotSerializable Error

Posted 01 April 2011 - 04:53 AM

Moved to Java Help. Please reserve the Java Programmers forum for topical, non-help related discussion.
Was This Post Helpful? 0
  • +
  • -

#4 xtr3mnico  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 89
  • Joined: 26-February 10

Re: List & Socket Problem, io.NotSerializable Error

Posted 01 April 2011 - 09:54 AM

I found the problem.
At first i did use the serializable interface, then i got a classnotfoundexception.

Solution

Both client and server must have the same package with the same name for each custom object.

All custom objects which are to be sent should implement serializable.

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: List & Socket Problem, io.NotSerializable Error

Posted 01 April 2011 - 02:23 PM

View Postxtr3mnico, on 01 April 2011 - 11:54 AM, said:

Both client and server must have the same package with the same name for each custom object.

Really ?
Was This Post Helpful? 1
  • +
  • -

#6 xtr3mnico  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 89
  • Joined: 26-February 10

Re: List & Socket Problem, io.NotSerializable Error

Posted 01 April 2011 - 02:52 PM

View Postpbl, on 01 April 2011 - 02:23 PM, said:

View Postxtr3mnico, on 01 April 2011 - 11:54 AM, said:

Both client and server must have the same package with the same name for each custom object.

Really ?


Yes, If not it will throw a Class Not Found Exception on both the Client and the Server, i got this error but i did not state it in my problem.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: List & Socket Problem, io.NotSerializable Error

Posted 01 April 2011 - 04:01 PM

View Postxtr3mnico, on 01 April 2011 - 04:52 PM, said:

View Postpbl, on 01 April 2011 - 02:23 PM, said:

View Postxtr3mnico, on 01 April 2011 - 11:54 AM, said:

Both client and server must have the same package with the same name for each custom object.

Really ?


Yes, If not it will throw a Class Not Found Exception on both the Client and the Server, i got this error but i did not state it in my problem.

I was sarcastic :) You obviously missed the whole concept of serialization
How can you expect a serialized object of some class and some package to be deserialized into another object of some package some class ?

This post has been edited by pbl: 01 April 2011 - 04:06 PM

Was This Post Helpful? 0
  • +
  • -

#8 xtr3mnico  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 89
  • Joined: 26-February 10

Re: List & Socket Problem, io.NotSerializable Error

Posted 01 April 2011 - 05:48 PM

Honestly no idea... but why would the package matter? if the connection[socket to client and vice versa] is correct why is there an extended security on the package on the receiving end.

I really was looking over my comment :dontgetit: cause i dont think someone like you would say something like that... :plain:

This post has been edited by xtr3mnico: 01 April 2011 - 06:20 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: List & Socket Problem, io.NotSerializable Error

Posted 03 April 2011 - 08:12 PM

View Postxtr3mnico, on 01 April 2011 - 07:48 PM, said:

Honestly no idea... but why would the package matter? if the connection[socket to client and vice versa] is correct why is there an extended security on the package on the receiving end.

I really was looking over my comment :dontgetit: cause i dont think someone like you would say something like that... :plain:

first concept of serialization is package name and class name then serialVersionUID
if these do not match serialization is useless

This post has been edited by pbl: 11 April 2011 - 06:12 PM

Was This Post Helpful? 2
  • +
  • -

#10 xtr3mnico  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 89
  • Joined: 26-February 10

Re: List & Socket Problem, io.NotSerializable Error

Posted 11 April 2011 - 08:59 AM

did not known that <_< now my solution seems dumb :dots:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1