1

i am trying to create a mock shopping cart for a uni project. im using two java classes Item and shoppingCart, shopping cart uses a vector to store Items and then writes them to a file. i am trying to use the classes on a jsp page but when i try to write to the file i get an java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: cart.Item... any ideas how i could fix this??

3 Answers3

2

From the javadoc on NotSerializableException... "Thrown when an instance is required to have a Serializable interface."

This means that your class that you're serializing need to implement the Serializable marker interface.

Yuliy
  • 17,381
  • 6
  • 41
  • 47
  • You could add that it needs a 'serialVersionUID' in order to be Serializable. – boutta May 05 '09 at 07:07
  • i have tried implementing Serializable on both classes with no luck –  May 05 '09 at 07:08
  • Does Item contain any members that are not serializable? Otherwise, I'd double-check that you are indeed implementing java.io.Serializable. As for serialVersionUID, you should have that, but it won't result in NotSerializableException if you do not provide it (the runtime will auto-calculate the version ID when needed). – Yuliy May 05 '09 at 16:21
0

Here is a small Example to you...

import java.io.*;

class player implements Serializable{
    String name;
    double health;
    double positionX;
    double positionY;
    int weapon;     
}


class game{
    public static void main(String[] args) throws Exception{

        player character1 = new player();
        character1.name = "Inukz";
        character1.health = 82.62;
        character1.positionX = 80;
        character1.positionY = 33;
        character1.weapon = 2;

        player character2 = new player();
        character2.name = "Prasad";
        character2.health = 32.62;
        character2.positionX = 40;
        character2.positionY = 63;
        character2.weapon = 3;


        player character3 = new player();
        character3.name = "Thilan";
        character3.health = 12.62;
        character3.positionX = 10;
        character3.positionY = 83;
        character3.weapon = 1;



        FileOutputStream fileStream = new FileOutputStream("myGame.js",true);
        ObjectOutputStream os = new ObjectOutputStream(fileStream);

        os.writeObject(character1);


        os.close();


    }

}
CodeMonkey
  • 2,828
  • 1
  • 23
  • 32
0

To avoid NotSerializableException make sure:

  1. your class implements Serializable
  2. all non primitive members implement Serializable (or are transient instead)
  3. if your class is an inner class it's either static or the outer class implements Serializable

Besides that you also need to define serialVersionUID for every Serializable class. Check all 3 cases above plus:

  1. all Serializable superclasses
  2. if your class is an anonymous class, define it there too

Note: your code may run without serialVersionUID sometimes but read the last paragraph in Serializable's javadoc to understand why it will be a problem depending on the environment.


There's a VM option to add details to the exception. It shows the root and nested classes failing to serialize:

-Dsun.io.serialization.extendedDebugInfo=true
Victor Basso
  • 5,556
  • 5
  • 42
  • 60