I'm writing objects in a link list to file and when I write more thatn a certain number I get a StackOverflowError. My list has Item objects which have long part numbers, two doubles, a string, and an int. here's the code that is failing;

try {
			FileOutputStream fos = new FileOutputStream(file1);
			BufferedOutputStream bos = 
				new BufferedOutputStream(fos);
			ObjectOutputStream oos = 
				new ObjectOutputStream(bos);
			oos.writeObject(itemInventory);
			oos.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}

This is the error i get;

Exception in thread "main" java.lang.StackOverflowError
at java.lang.Double.doubleToLongBits(Double.java:774)
at java.io.Bits.putDouble(Bits.java:119)
at java.io.ObjectStreamClass$FieldReflector.getPrimFieldValues(ObjectStreamClass.java:1919)
at java.io.ObjectStreamClass.getPrimFieldValues(ObjectStreamClass.java:1184)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1494)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)

....
and it repeats at this point.

First of all if you look at the API of the classes: ObjectOutputStream, ObjectInputStream, you will find examples on how to save and read objects. Use those.

Also what type is the itemInventory and does it implement the java.io.Serializable interface?

This article has been dead for over six months. Start a new discussion instead.