反序列化后将对象添加到集合中时出错

时间:2017-06-15 15:47:01

标签: java serialization collections

我无法弄清楚我的计划有什么问题。有3个类,Owner是单例,因此当GUI请求Owner实例时,getOwner()中的Owner方法会返回先前保存的Owner ,否则返回新的Owner。我认为序列化是正确实现的。 Map cars集合是暂时的,退出时无需保存。

当程序第一次运行时,用户可以通过按下gui中的任意一个按钮来执行intoMap1()intoMap2()方法,其中Owner方法在addCar()方法中要求Map创建汽车并使用键A1或/和B2插入其Map集合。我检查过每个程序运行后cars Owner包含1或2辆汽车,所以没有问题。

当程序关闭并再次打开时,Cars.ser已保存并在addCar()文件中反序列化且没有错误,我用print语句检查了它。问题是当程序再次运行Ownercars创建新车,但没有将其添加到Map cars,这会导致异常。地图A1未保存,因此它是空的,无论如何即使在第一次运行中我插入地图车B2,它也不会在第二次运行中插入新车MapCars.ser是暂时的,有什么不对吗?为什么它不能在第二轮中添加汽车来映射?如果我删除Map文件,则在第一次运行时再次添加汽车。似乎在第二轮中name;不想合作。在第二次运行中,使用先前保存的字段Owner创建新对象。散列是否会导致问题?

仅在cars.containsKey(regNumIn)第34行的第二次运行中导致异常,public class GUI extends javax.swing.JFrame { Owner owne; public GUI() { owne = Owner.getOwner(); //name is correctly retrieved each time System.out.println(owne.toString()); //name is printed each time addWindowListener(new CloseQuit()); initComponents(); } public void intoMap1() //executed by pressing button in gui, asking owne object to create and add car { owne.addCar("A1", "red", 6); //Line 36 } public void intoMap2() //executed by pressing button in gui, asking owne object to add cars { 41 owne.addCar("B2", "black", 4); //Line 41 } private class CloseQuit extends WindowAdapter { public void windowClosing(WindowEvent e) { System.out.println("CloseQuit executed"); owne.saveOwner(); } } } 在第45行因某种原因失败。

public class Owner implements Serializable
{
    String name;    
    transient Map<String, Car> cars;

    public Owner()
    {
        name = "John";
        cars = new HashMap<>();
    }

    public void addCar(String regNumIn, String colourIn, int doorsIn)
    {
        Car car = new Car(regNumIn, colourIn, doorsIn); //works in each execution 
        System.out.println("Car is created" + car.toString());              
         try
         {
34           cars.put(regNumIn, car);  //Line 34, works only when program runs first time, 
                                       //after Owner is deserialized it doesnt add cars with new keys, exception
         }
         catch (Exception ex)
         {
             System.out.println("Exception in Owner when adding car "); 
             ex.printStackTrace();   
         }

        try
        {        
45          System.out.println("Map has key " + regNumIn  + ":  " + cars.containsKey(regNumIn)); //Line 45
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

        System.out.println("--------------------------------------------------------------------");
    }

    public static Owner getOwner()  //works fine
    {
        Owner owne = null;
        try
        {                                        
            FileInputStream fis = new FileInputStream("Cars.ser");                                                                    
            ObjectInputStream ois = new ObjectInputStream(fis);                                                                          
            owne = (Owner) ois.readObject(); ois.close();       
        }
        catch(FileNotFoundException ex)
        {
            owne = new Owner(); //creates new Owner if run for first time  
        }
        catch (Exception ex)
       {                
           System.exit(0);
       }            
       return owne;
   }

   public void saveOwner()  //works fine 
   {
       try
       {
            FileOutputStream fis = new FileOutputStream("Cars.ser"); 
            ObjectOutputStream ois = new ObjectOutputStream(fis);
            ois.writeObject(this); ois.close();
       }
       catch(Exception ex)
       {
           System.out.println("Can't store state of owner");  
           System.exit(0);
       }
   }

   @Override
   public String toString()
   {
       return ("name = " + name);
   }
}  
public class Car 
{
    String regNum;
    String colour;
    int doors;

    public Car(String regNumIn, String colourIn, int doorsIn)
    {
        regNum = regNumIn;
        colour = colourIn;
        doors = doorsIn;
    }    

    public String toString()
    {
        return ("regNum = " + regNum );
    }
} 
run:
--------------
name = John
--------------
Car is created: regNum = A1
Map has key A1:  true
--------------------------------------------------------------------
Car is created: regNum = B2
Map has key B2:  true
--------------------------------------------------------------------
CloseQuit executed
BUILD SUCCESSFUL (total time: 8 seconds)

首次运行后输出为:

   run:
--------------
name = John
--------------
Car is created: regNum = A1
Exception in Owner when adding car
java.lang.NullPointerException
    at core.Owner.addCar(Owner.java:34)
    at gui.GUI.intoMap1(GUI.java:36)
    at gui.GUI.jButton1ActionPerformed(GUI.java:215)
    at gui.GUI.access$100(GUI.java:17)
    at gui.GUI$1.actionPerformed(GUI.java:87)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6535)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
--------------------------------------------------------------------
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
java.lang.NullPointerException
    at core.Owner.addCar(Owner.java:45)
    at gui.GUI.intoMap1(GUI.java:36)
    at gui.GUI.jButton1ActionPerformed(GUI.java:215)
    at gui.GUI.access$100(GUI.java:17)
    at gui.GUI$1.actionPerformed(GUI.java:87)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6535)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Car is created: regNum = B2
Exception in Owner when adding car
java.lang.NullPointerException
    at core.Owner.addCar(Owner.java:34)
    at gui.GUI.intoMap2(GUI.java:41)
    at gui.GUI.jButton4ActionPerformed(GUI.java:230)
    at gui.GUI.access$400(GUI.java:17)
    at gui.GUI$4.actionPerformed(GUI.java:126)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6535)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
--------------------------------------------------------------------
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
java.lang.NullPointerException
    at core.Owner.addCar(Owner.java:45)
    at gui.GUI.intoMap2(GUI.java:41)
    at gui.GUI.jButton4ActionPerformed(GUI.java:230)
    at gui.GUI.access$400(GUI.java:17)
    at gui.GUI$4.actionPerformed(GUI.java:126)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6535)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6300)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4891)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2750)
    at java.awt.Component.dispatchEvent(Component.java:4713)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
CloseQuit executed
BUILD SUCCESSFUL (total time: 15 seconds)

第二次运行输出后:

g>2

0 个答案:

没有答案