cast Exception.

2006-04-11
2013-06-03
  • Africancoder

    Africancoder - 2006-04-11

    Dear all,

    I have file with object type ="myclass"
    Now I want to read the record one by one and cast it to another type
    (i.e.(yourclass)tuple.getValue()). 
    Cast Class Exception thrown.  Anyone have a good idea how to do it?

    Reason not casting to the original class:
    The original class does not have a get method for the private variable.  However, need to create something like "data explorer" tool to get the private variable for the input file.

    regards,
    Coder

     
    • Kevin Day

      Kevin Day - 2006-04-11

      Please post:

      Code segment showing how you are inserting values into the BTree (I'm assuming you are using a BTree to store your values).

      Code segment showing how you are retrieving values from the BTree, including the exact code tht is involved in the cast.

      Finally, please post the full exception stack trace - it should include the class of the object returned by tuple.getValue(), which will tell you a lot about whether you are casting to the same type that you actually stored.

      - K

       
      • Africancoder

        Africancoder - 2006-04-11

        Here is the source

        import java.util.Properties;

        import jdbm.RecordManager;
        import jdbm.RecordManagerFactory;
        import jdbm.btree.BTree;
        import jdbm.helper.StringComparator;
        import jdbm.helper.Tuple;
        import jdbm.helper.TupleBrowser;

        public class a implements java.io.Serializable{
          class secretclass implements java.io.Serializable{
             private String key1=null;
             private String key2=null;
            
             public secretclass(String key1, String key2) {
                super();
                // TODO Auto-generated constructor stub
                this.key1 = key1;
                this.key2 = key2;
            }
             public String getkey1(){
                 return key1;
             }
             // there are another method to get and set key2 value
             // but too complicate to include here.
        }
        class myclass implements java.io.Serializable{
             private String key1=null;
             private String key2=null;
             public String getkey1(){
                 return key1;
             }
             public String getkey2(){
                 return key2;
             }
        }

        public void initdata(){
             Properties props = new Properties();
             BTree tree;
                try {
                    secretclass inputrec=new secretclass("v1","v2");
                    RecordManager recMan1 = RecordManagerFactory.createRecordManager("c:/test",props);
                    long recid=recMan1.getNamedObject("test");
                    tree=BTree.createInstance(recMan1,new StringComparator());
                    recMan1.setNamedObject("test",tree.getRecid());
                    tree.insert("1",inputrec,false);
                    recMan1.commit();
                   
                    recMan1 = RecordManagerFactory.createRecordManager("c:/test",props);
                    recid=recMan1.getNamedObject("test");
                    tree=BTree.load(recMan1,recid);
                    System.out.println("load data");
                    TupleBrowser browser = tree.browse();
                    Tuple tuple = new Tuple();
                    while (browser.getNext(tuple)) {
                        myclass b=(myclass)tuple.getValue();
                        System.out.println("output"+b.getkey2());
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
               
        }
        public static void main(String[] args) {
             a mya=new a();
             mya.initdata();
        }
        }

        Here is the stack trace
        java.lang.ClassCastException: a$secretclass
            at a.initdata(a.java:59)
            at a.main(a.java:69)

         
        • Kevin Day

          Kevin Day - 2006-04-11

          OK - the exception says it all:  You are trying to cast an object that is a$secretclass to type a$myclass.  Because secretclass does inherit from myclass, that cast is not allowed.

          One other general comment:

          You are not using standard style conventions for Java.  Name your classes in upper leading case (i.e. SecretClass, MyClass).  Following standard conventions makes it much easier for others to read your code.  Anyway, a useful resource for this is:  http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html#367

          Cheers,

          - K

           
    • Africancoder

      Africancoder - 2006-04-12

      Thanks for your advice.  Forgive me about the coding standard.  I wrote it just after getting your reply.  The original source code involve too many class...

      The question is:
      Now I want to read the record one by one and cast it to another type 
      (i.e.(yourclass)tuple.getValue()). 
      Cast Class Exception thrown. Anyone have a good idea how to do it?

      It should have, the input file is only the serialized object.  There should be a way to cast it to another type. 

      regards,
      coder

       
      • Kevin Day

        Kevin Day - 2006-04-12

        I think you may have some mis-understandings about how Java serialization works.

        I would recommend that you try to get your code working with writing the object to a file, then reading it back from the file using ObjectOutputStream and ObjectInputStream.  Once you have that working, then you can turn to using jdbm as the data store instead of a simple file.

        Here is a tutorial on Java serialization that may be helpful:

        http://java.sun.com/docs/books/tutorial/essential/io/serialization.html

        - K

         
    • Africancoder

      Africancoder - 2006-04-13

      Thanks again.
      I solved the problem by using reflection to retrieve the private variable value.

      regards,
      Coder.

       
      • Kevin Day

        Kevin Day - 2006-04-13

        Any reason you don't just cast the returned value to secretclass, and access things that way?  Presumably, you have access to the SecretClass source... or are you trying to read a jdbm file that someone else created?

        - K

         

Log in to post a comment.