13 Replies Latest reply: May 13, 2010 3:44 AM by 794822 RSS

    Deserialize works fine. Now I cannot create my class instance

    794822
      When I deserialize my object a can get the object. Ok.

      ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fichero));     
                     aux = ois.readObject();                
                     System.out.println(aux);
      And I see :
      mypackage.myclass
      (This class is a Jpanel based and some references to another classes)


      Now I have 3 questiosn - problems:
      1.- I dont know the class name (or I want that my code works with every kind of object) ,so... Is it neccesary to CAST the object?
      2.- In this case.... it can be done by mean of string name ? aux = (the_cast) .... ( when the_cast = "MYclass")
      3.- Finally I dont know how to create an instance of my new class
      ((MY_CAST) aux.getclass()).newinstance(); throws an error of "java.lang.Class cannot be cast ..."

      Any help?
        • 1. Re: Deserialize works fine. Now I cannot create my class instance
          843790
          tonnot wrote:
          Now I have 3 questiosn - problems:
          1.- I dont know the class name (or I want that my code works with every kind of object) ,so... Is it neccesary to CAST the object?
          No, it's not necessary usually and when you don't know the type at compile time, then it's not possible either.
          2.- In this case.... it can be done by mean of string name ? aux = (the_cast) .... ( when the_cast = "MYclass")
          No, that's not possible. Since casting doesn't change anything about the object but only about the reference, casting "to a dynamic type" is meaningless, since you couldn't have a reference variable "of a dynamic type" to hold the newly-cast object anyway.
          3.- Finally I dont know how to create an instance of my new class
          ((MY_CAST) aux.getclass()).newinstance(); throws an error of "java.lang.Class cannot be cast ..."
          The problem is that you're trying to cast the Class object to MY_CAST (which I assume is your class), while it should in fact be a Class<? extends MY_CAST>.

          I'm unclear what you want to achieve here and what you know about the object: Why do you want to create a new instance of an object you know nothing about?
          • 2. Re: Deserialize works fine. Now I cannot create my class instance
            794822
            Thanks ...
            Can ypu help me again ? This is the new code ....
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fichero));     
            +          aux = ois.readObject();+
            +// aux is read ok !!!!+
            +          return aux.getClass().newInstance();+
            And the list line doesnt run, I receive :
            Can not call newInstance() on the Class for java.lang.Class
            Now, How can I create the instance ?

            Answering your question ... My intention is to write a program to serialize - deserialize any class. I know which serialized class I want because I know the name of the file I want to deserialize....
            • 3. Re: Deserialize works fine. Now I cannot create my class instance
              843790
              tonnot wrote:
              My intention is to write a program to serialize - deserialize any class.
              You already did that! During deserialization a new object was already created!
              I know which serialized class I want because I know the name of the file I want to deserialize....
              Ok, but why do you think you need to call newInstance() on anything in order to deserialize? There's no need to do that.
              • 4. Re: Deserialize works fine. Now I cannot create my class instance
                794822
                I dont know how to use this object
                (this object is Jpanel based and a pass to a method that use component cast)

                and If a use return aux.getClass() or retrurn aux
                When I try to use this class using :
                the_dialog.add((Component) aux);
                i receive :
                java.lang.Class cannot be cast to java.awt.Component

                What more I must to do ?
                And... Thank you again
                • 5. Re: Deserialize works fine. Now I cannot create my class instance
                  Kayaman
                  tonnot wrote:
                  and If a use return aux.getClass()
                  You do realize that will always return an object of class Class, right?
                  • 6. Re: Deserialize works fine. Now I cannot create my class instance
                    794822
                    I dont understand which is the differences betwen objecto and object.getclass()
                    But my wish is to use it ....
                    In both cases aux and aux.getclass() when I try to use it passing to a method a Receive :
                    java.lang.Class cannot be cast to java.awt.Component

                    As I explained before, my Object is Jpanel based. I want to cast it to (Component) in orden it can be added to a Frame.

                    I this the object is correctly retreived isn't it ? Now, why it does not run ....
                    Must I create and Instance ?

                    Please .... I need more help ...
                    • 7. Re: Deserialize works fine. Now I cannot create my class instance
                      794822
                      Perhaps serialize classes with swing components are the problem ?
                      I have read that it is recommended to use XMLencoder to do this, is it true ?
                      By the moment I go to stop learn about serialization-deserialization
                      Again I little java obstacle.....
                      • 8. Re: Deserialize works fine. Now I cannot create my class instance
                        843790
                        tonnot wrote:
                        Perhaps serialize classes with swing components are the problem ?
                        No, you have a much more basic problem.
                        I have read that it is recommended to use XMLencoder to do this, is it true ?
                        That is true. But I don't think that this particular issue is the problem you face right now.
                        By the moment I go to stop learn about serialization-deserialization
                        My suggestion: you should learn about serialization in a context outside of a current problem.

                        You should start with simple example programs that only serialize simple object (a String, for example) and then go up from there.

                        At the moment you try to jump into the deep end of the pool without ever dipping your toe in the water ...
                        • 9. Re: Deserialize works fine. Now I cannot create my class instance
                          794822
                          I have discovered that I must write and read an instance of MYclass
                          it can be done with this 2 methods:

                          1.- MYClass d = new MYclass;
                          2.- Object d = Class.forName("mypackage.MYclass").newInstance()

                          In this situation before to read the file I can create the object with
                          MYclass d = (MYclass) s.readObject(); (s is the ObjectInputStream)
                          Ok !!

                          When a used the simple class, with Class<?> d = Class.forName("MYclass");
                          the file was written correctly and can be read using ObjectInputStream
                          Now I have a doubt, what is the content of this file ?
                          What is I read ? If is it a class, how can i create the object ?
                          • 10. Re: Deserialize works fine. Now I cannot create my class instance
                            EJP
                            it can be done with this 2 methods:

                            1.- MYClass d = new MYclass;
                            2.- Object d = Class.forName("mypackage.MYclass").newInstance()

                            In this situation before to read the file I can create the object with
                            MYclass d = (MYclass) s.readObject(); (s is the ObjectInputStream)
                            Ok !!
                            OK. So why do you need 1 and 2?
                            What is I read?
                            Whatever you wrote to it. What did you write to it?
                            • 11. Re: Deserialize works fine. Now I cannot create my class instance
                              794822
                              Finally I get it!!!
                              This is the code to serialize - deserialize a class, rather than an object, without instantiation.

                              1.- I pass 'mypackage.myclass' as a string 'classtoa' and :
                              ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
                              Class<?> p = Class.forName(classtoa);     
                              oos.writeObject(p);
                              oos.flush();
                              oos.close();

                              2.- I retrieve the class by filename :
                              ObjectInputStream ois = new ObjectInputStream(new FileInputStream(the_file));     
                              class <?>auxclass = (Class<?>) ois.readObject();
                              Object aux = auxclass.newInstance();
                              return aux;
                              aux can be cast to a JComponent (for example) and use the .setVisible


                              I Think this way is usefull to achive classes (and send and retrieve it trought the net)
                              As you can see We only must to know 1.- The class to achive 2.- The file to retrieve. Both can be passed as a string.
                              Now I'm going to try if it can be posible to load and can serve to test diferent versions of a class.

                              Any opinion ?
                              Thanks
                              • 12. Re: Deserialize works fine. Now I cannot create my class instance
                                jtahlborn
                                this is effectively meaningless. first of all, you aren't really storing the class, just the name of it. the class data still needs to exist at the source and dest. secondly, i'm guessing you want to store instances of classes, not the class itself (most often the point of serialization is storing the current state of an instance of a class).