0

Updated code shown as: //1, //2, //3, //4, //5.

I would like to use ObjectInputStream and ObjectOutputStream to write and read text file. Using this tutorial I tried to implement it to my class:

       //Serializable Employee class
        public class Employee implements java.io.Serializable
        {
           public String name;
           public String name2;

           public ArrayList <String> arr; //1
        }
                  .....

        //ObjectOutputStream class

            Employee e = new Employee();
            e.name = jTextField1.getText();

            for (int i = 0; i < jPanel1.getComponentCount(); i++) {
                    SubPanel panel = (SubPanel) jPanel1.getComponent(i);
                    JTextField tf = panel.getTf();

                    e.arr.add(tf.getText()); //2
                    String b = e.arr.get(i); //3
                    System.out.println(b);   //4        
            }

          try
          {
             FileOutputStream fileOut = new FileOutputStream("test.ser");
             ObjectOutputStream out = new ObjectOutputStream(fileOut);
             out.writeObject(arr);
             out.close();
             fileOut.close();
             System.out.printf("Serialized data is saved in test.ser");
          }catch(IOException i)
          {
              i.printStackTrace();
          }
                 .....

        //ObjectInputStream class
          Employee e = null;
      try
      {
         FileInputStream fileIn = new FileInputStream("test.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         e = (Employee) in.readObject();
         in.close();
         fileIn.close();
      }catch(IOException i)
      {
         i.printStackTrace();
         return;
      }catch(ClassNotFoundException c)
      {
         System.out.println("Employee class not found");
         c.printStackTrace();
         return;
      }
          jTextField1.setText(e.name);

          for (int i = 0; i < jPanel1.getComponentCount(); i++) {
                SubPanel panel = (SubPanel) jPanel1.getComponent(i);
                JTextField tf = panel.getTf();

                tf.setText(e.arr.get(i));  //5
        }
    }

But in ObjectOutputStream class in this field e.name2 = tf.getText(); if after adding 2 dynamic tf I type 2, 3, but output test.ser write only the value 3, and when I read test.ser file, then on dynamic tf I am getting values 3, 3.

I would like to ask how can I write and read dynamically added tf values?

And output I am getting this error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Test2.Test.serActionPerformed(Test.java:2331)
at Test2.Test.access$800(Test.java:47)
at Test2.Test$9.actionPerformed(Test.java:591)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
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:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
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)
hub
  • 101
  • 10

1 Answers1

2

If I understood your question correctly, you are overwiriting the previous value stored in e.name2 in each iteration of the loop.

So, When you deserialize you will get only the last written value to the filed e.name2.

For holding multiple values, use ArrayList<String> in place of e.name2.

Bhargav Kumar R
  • 2,190
  • 3
  • 22
  • 38