0

I've been trying to work out saving and loading objects in Java, but I'm having some problems with the loading of the file. What I want to do here is load an ArrayList of objects called Employee, which I have serialized. I'm still getting a red wall of errors when I try to load it. What am I doing wrong? Here is my code:

try (FileInputStream filStrom = new FileInputStream(filpeker);
                        ObjectInputStream objektStrom = new ObjectInputStream(filStrom))
                    {
                    employeelist = (ArrayList<Employee>)objektStrom.readObject();
                    objektStrom.close();
                } catch (IOException | ClassNotFoundException e) {
                    JOptionPane.showMessageDialog(null, "Feil under lesing av fil! " + e.getMessage());
                    e.printStackTrace();
                } 

The errors I'm getting are these:

java.io.InvalidClassException: ovinger9.Employee; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(Unknown Source)
at java.io.ObjectStreamClass.checkDeserialize(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.util.ArrayList.readObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at ovinger9.EmployeeGUI$2.actionPerformed(EmployeeGUI.java:135)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
5
  • 2
    Looks pretty explicit to me: ovinger9.Employee; no valid constructor Commented Apr 3, 2013 at 20:06
  • @JimGarrison ...newInvalidClassException... Commented Apr 3, 2013 at 20:07
  • In the Employee class make sure you have the default constructor, also note that if you have an overridden constructor, then you have to explicitly add the default constructor. Commented Apr 3, 2013 at 20:09
  • Sounds like a job for a marshalling/unmarshalling framework - they work with plain text as well as XML Commented Apr 3, 2013 at 20:16
  • A no arg constructor is not necessarily a requirement for serialization. Commented Apr 3, 2013 at 20:36

2 Answers 2

3

Add public constructor that takes no parameters to the ovinger9.Employee class (in addition to another constructor you already have there).

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for Keen eye and good memory...
1

This could happen if your Employee class extends a class that is not Serializable, and the superclass does not have a zero argument constructor, as decribed in the java.io.Serializable documentation. To solve it, either declare your super class as Serializable, or add a zero arg constructor to the super class.

1 Comment

I figured it out with your advice thanks! I had completely forgot about the superclass of Employee which I hadn't serialized.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.