blue sky
blue sky

Reputation: 161

Object ArrayList loses String values after deserialization?

so i have a three class video store GUI and it is supposed to save records of videos in stock. however it saves the video objects using serialization but for some reason ,even though i am not getting any errors, only the numerical values are making it through..

enter image description here

notice how the leftmost three columns and the rightmost column are all empty. this is because they are meant to have strings in them, and yet they dont...

as i said im not getting any errors so this truly confuses me.

constructor of VideoStore.java(the GUI class):

public VideoStore() {
        initComponents();
        model = (DefaultTableModel)displayVideos.getModel();
        try{
        BinaryFile = new BinaryFile();
        BinaryFile.load();
        }
        catch(Exception e){

        }
            for(int j = 1; j < BinaryFile.videosList.size(); j ++) {
            Video load = (Video)BinaryFile.videosList.get(j);
            String tempName = load.getVideoName();
            String tempProd = load.getProducer();
            String tempRat = load.getRating();
            String tempGenre = load.getGenre();
            short tempNum = load.getVidNum();
            float tempPrice = load.getvideoPrice(); 

            try {
                Object[] row = {ID, tempName, tempProd, tempGenre, tempPrice, tempNum, tempRat};
                model.addRow(row);
            } catch(Exception e){

            }

            ID++;
        }
    }

and then the BinaryFile class that i use to handle the .ser file:

public void load(){
        try
      {
         FileInputStream fileIn = new FileInputStream("/Users/hanaezz/Desktop/output.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         videosList = (ArrayList)in.readObject();
         in.close();
         fileIn.close();
      } catch(Exception i)
      {
         i.printStackTrace();
         return;
      }
    }

    public static void adderoo(Video v) {
        videosList.add(v);
    }

finally, the video class that is in the ArrayList:

private static String videoName;
private static String producer;
private static String rating;
private static String genre;
private short videoNumber;
private float videoPrice;

Upvotes: 0

Views: 353

Answers (1)

Matteo Baldi
Matteo Baldi

Reputation: 5828

Static variabled are NOT serialized, you should put:

private String videoName;
private String producer;
private String rating;
private String genre;
private short videoNumber;
private float videoPrice;

in your video class. The only static variable you should put in a Serializable class is serialVersionUID (which is used by the serialization and deserialization process). As in:

private static final long serialVersionUID = 1L;

Upvotes: 6

Related Questions