2
public static void addToFile(LinkedList<Carowners> carowner)
{
    try
    {
    File file = new File(filename);
        FileOutputStream fout= new FileOutputStream(file);
        ObjectOutputStream out= new ObjectOutputStream(fout);
        out.writeObject(carowner);
        out.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

}

Hi this is my code.I am getting following error when i am object serialization.I implemented serializable, for the classes.

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: 

Billing.Person
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    at java.util.LinkedList.readObject(LinkedList.java:1136)
    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.invokeReadObject(ObjectStreamClass.java:1004)
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1872)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1777)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    at Billing.Carownerslist.<init>(Carownerslist.java:42)
    at Billing.Carownerslist.main(Carownerslist.java:253)
Caused by: java.io.NotSerializableException: Billing.Person
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
    at java.util.LinkedList.writeObject(LinkedList.java:1118)
    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:975)
    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 Billing.Carownerslist.addToFile(Carownerslist.java:242)
    at Billing.Carownerslist.addCompany(Carownerslist.java:160)
    at Billing.Carownerslist.main(Carownerslist.java:285)
Exception in thread "main" java.lang.NullPointerException
    at Billing.Carownerslist.addCompany(Carownerslist.java:149)
    at Billing.Carownerslist.main(Carownerslist.java:278)
Java Result: 1
Toby Allen
  • 10,997
  • 11
  • 73
  • 124
  • What is in this line? `Billing.Carownerslist.addCompany(Carownerslist.java:149)` – donfuxx Mar 02 '14 at 19:45
  • @donfuxx Ihas parameters of type string. – gowthamganguri Mar 02 '14 at 19:48
  • I would like to take a look at your `addCompany` method because there an NPE is thrown – donfuxx Mar 02 '14 at 19:49
  • Caused by: java.io.NotSerializableException: Billing.Person. The class Carowners must be containing an instance of Billing.Person, which is not serializable. – maress Mar 02 '14 at 19:50
  • @maress and before that is an NPE – donfuxx Mar 02 '14 at 19:51
  • @donfuxx public boolean addCompany(String name,String address,String vat) { Company p =new Company(name, address, vat); for (Carowners cown :carowners) { if(cown instanceof Company) { if(((Company)cown).vat.equals(vat)) { return false; } } } carowners.add(p); addToFile(carowners); return true; } – gowthamganguri Mar 02 '14 at 19:53
  • @gowthamganguri Can you post your main method here. There seems to be two exceptions here, NPE and then serialization – maress Mar 02 '14 at 19:55
  • @maress public static void main(String[] args) { Carownerslist col = new Carownerslist(); if( col.addCompany("asdf", "qwert", "123")) { System.out.println("Company added"); } if(col.addcarToCompany("123", "sdfg", 1990, "123")) { System.out.println("car added"); } if( col.addCompany("asdf", "qwert", "456")) { System.out.println("2 Company added"); } } – gowthamganguri Mar 02 '14 at 19:57
  • @gowthamganguri is this the complete error trace? NPE must be occurring at the first call to addCompany (which since it does not end the program, you are catching it somewhere) and then serialization exception occurs at the second call to addCompany, which you must be trying to serialize after. those two exceptions are different. You may post all the relevant code in order to see whats the problem – maress Mar 02 '14 at 20:04
  • @maress,@donfuxx.. Thank you.. i took care of null pointer exception. File was written with exceptions when i run first time .and it is being read everytime not initializing the arraylist. It got rectified now... – gowthamganguri Mar 02 '14 at 20:13

1 Answers1

0

The exception is in the stream. You have to recreate the stream now that you've made the class Serializable.

user207421
  • 305,947
  • 44
  • 307
  • 483