• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.io.NotSerializableException and java.io.WriteAbortedException

 
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

please have a look at the following three set of codes, which is using the "Vector" class.

Publishers.java



Server.java



Client.java


The code is running fine, untill the Client.java is being executed. As soon as that code started, the Server.java throws the following exception

java.io.NotSerializableException: Publishers
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1362)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1170)
at java.io.ObjectOutputStream.access$300(ObjectOutputStream.java:161)
at java.io.ObjectOutputStream$PutFieldImpl.writeFields(ObjectOutputStream.java:1687)
at java.io.ObjectOutputStream.writeFields(ObjectOutputStream.java:478)
at java.util.Vector.writeObject(Vector.java:1073)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:962)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at Server.run(Server.java:65)



The Client.java throws the following exception

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: Publishers
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1685)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1341)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1888)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at Client.init(Client.java:49)
at sun.applet.AppletPanel.run(AppletPanel.java:434)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.io.NotSerializableException: Publishers
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1362)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1170)
at java.io.ObjectOutputStream.access$300(ObjectOutputStream.java:161)
at java.io.ObjectOutputStream$PutFieldImpl.writeFields(ObjectOutputStream.java:1687)
at java.io.ObjectOutputStream.writeFields(ObjectOutputStream.java:478)
at java.util.Vector.writeObject(Vector.java:1073)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:962)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1480)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at Server.run(Server.java:65)




I guess this is done by the Vector class. Anyway I know I should not use this class because it is replaced by the ArrayList, but we HAVE to use it. Please help me to find this error and correct it. Thanks


 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Managed to solve by my self. Thanks for everyone who viewed this trying to help me.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yohan,

Even when you solve your own problem, it's nice if you post the answer here so everyone can learn from it. In this case, I assume you just added "implements Serializable" to your definition of Publisher. Did you have to do anything else?

Greg
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for not posting the answer Greg. I apologize for that.


In this case, I assume you just added "implements Serializable" to your definition of Publisher. Did you have to do anything else?


Of course, that is what I did. Apart from that I had to set the "VECTOR" implementation like this

Vector<Publishers>records = new Vector<Publishers>();

Heard mentioning the EXACT object is better, that's why I did that.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is kind of off the point but i cant help but notice how different that code is from java 2 code. the generics.

i always hated casting
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, generics have been a nice addition to Java. Actually, another is ArrayList, which is a growable array that implements the java.util.List interface. It's pretty much replaced Vector for this purpose. Vector is synchronized, and performs slowly compared with ArrayList. Most of the time, you're only accessing your lists from a single thread anyway, so synchronizing is a wasted effort.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randall Twede wrote:this is kind of off the point but i cant help but notice how different that code is from java 2 code. the generics.

i always hated casting



You mean my code is good? I am happy then
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Charles wrote:Yes, generics have been a nice addition to Java. Actually, another is ArrayList, which is a growable array that implements the java.util.List interface. It's pretty much replaced Vector for this purpose. Vector is synchronized, and performs slowly compared with ArrayList. Most of the time, you're only accessing your lists from a single thread anyway, so synchronizing is a wasted effort.



Yes, my lecturer also said the same. Anyway as I mentioned in my first post, I used Vactor because we MUST develop this using vector (this is a homework). Anyway, I don't know, but isn't vector easier than the ArrayList?
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd have to define what you mean by easier. I don't think Vector is easier that ArrayList, but I haven't used it for something like a decade now. Whoever is forcing you to use Vector is way out of date with the current state of Java. Give me their contact info and I'll yell at them for you.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Charles wrote:You'd have to define what you mean by easier. I don't think Vector is easier that ArrayList, but I haven't used it for something like a decade now. Whoever is forcing you to use Vector is way out of date with the current state of Java. Give me their contact info and I'll yell at them for you.



Actually I am learning J2EE, you know, these are in there. Anyway, no matter, I am always upto date !!! I even did a research on latest packages in JAVA 7 and tested some of them too!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic