12 Replies Latest reply: Mar 10, 2009 12:45 PM by 800313 RSS

    Serializable

    800313
      I am trying to implement saving in my program. I have a JFileChooser which pops up and then sends the choosen path and name to the below method.

      I have one main class which controls all other classes called BMCalculator which implements Serializeable , I am sending an object of this class to the below method as well

                          public static void writeObjectToFile(String Filename, Object myobject){
                              FileOutputStream fos = null;
                              ObjectOutputStream out = null;
                              try{
                                  System.out.println("Saving too "+Filename+".ser");
                                  fos = new FileOutputStream(Filename+".ser");
                                  out = new ObjectOutputStream(fos);
                                  out.writeObject(myobject);
                                  out.close();
                               }catch(IOException ex){
                                  System.out.println("Writing to file ERROR: "+ex);
                               }
                          }//end of method
      I keep getting the below error message though
      Writing to file ERROR: java.io.NotSerializableException: java.awt.BasicStroke
        • 1. Re: Serializable
          807588
          BMCalculator
          Apparently, that class has a reference to a java.awt.BasicStroke instance that hasn't been marked as transient.

          ~
          • 2. Re: Serializable
            807588
            I had a BM this morning. No calculation ensued, however.
            .
            .
            .
            .
            Oh! BMI! Never mind...
            • 3. Re: Serializable
              800313
              Thanks the program successfully saves and opens now, but there is still one problem

              i read in the main class BMCalculator and set it to the be the current object
              this.bmCalculator = (BMCalculator)readObjectfromFile(filename);
              but if i change some of the gui components eg write in a textfield this appears empty when i open the program again even though if i do

              System.out.println(bmCalculaotr.getAnyVariable());

              it print out the correct variable. Do i have to update all the gui components with the data?

              Calypso
              • 4. Re: Serializable
                800313
                Experimenting more i have found that all the gui components actually contain the correct values e.g
                System.out.println(myTextField.getText());
                will print out the correct value but the textfield does not show it

                Why is this?

                Thanks in advance
                • 5. Re: Serializable
                  800313
                  With regard to my original question i occasionally get the following error
                  Writing to file ERROR: java.io.NotSerializableException: javax.swing.JTable$CellEditorRemover
                  I have a JTable in my gui but I dont think I have used a CellEditorRemover, I dont even know what one is. any ideas

                  Thanks

                  Calypso
                  • 6. Re: Serializable
                    807588
                    Simply serializing GUI components isn't a particularly good way of persisting data. Quite aside from the problems you're hitting, it also shows that you haven't got a clean separation of model and UI. It's the data you put in the UI that you want to persist in some way
                    • 7. Re: Serializable
                      807588
                      georgemc wrote:
                      Simply serializing GUI components isn't a particularly good way of persisting data.
                      Understatement detection.
                      Level: major
                      • 8. Re: Serializable
                        800313
                        Ok point taken, i am now going to serlize the model data. The data consists of several arrays and vectors. Is it best to write each array/vector to their own file or is it best to write all arrays/vectors to a single file. Is the latter even possible?

                        Thanks again
                        • 9. Re: Serializable
                          807588
                          Writing parts to separate files is crazy talk. And while its possible to do multiple writeObject calls to an ObjectOutputStream (and do the corresponding multiple readObject calls in the other direction), I prefer to define a serializable AppModel class that encapsulates all that data, and simply writing it with a single call to writeObject .
                          • 10. Re: Serializable
                            800313
                            Ok thanks im trying to save it all to one file now but i keep getting the following error, below I have listed what code i am using
                            //when click save
                            Object[] dataToWrite = {getTextFieldArrayData(),bmc.getTablesData(), bmc.getTablesColumnHeaders()};
                            Utils.writeObjectToFile(bmc.getSavePath(), dataToWrite);
                            
                            
                            //when click open
                            String[] textFieldInfo = (String [])programData[0];
                            ArrayList<Vector> tableData = (ArrayList<Vector>)programData[1];
                            ArrayList<Vector> columnNames = (ArrayList<Vector>)programData[2];
                            
                            //get the following error
                            Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.String;
                            any ideas
                            • 11. Re: Serializable
                              807588
                              I'm guessing because your error message was mysteriously without line numbers, but it looks like the fault of:
                              String[] textFieldInfo = (String [])programData[0];
                              The message implies programData[0] is a String, not an array of strings.
                              • 12. Re: Serializable
                                800313
                                Thank for all your help, managed to sort it out now