2 Replies Latest reply on Jan 2, 2008 9:26 PM by 807601

    class cast exception error

    800328
      when ever i compile this program, it gives me class cast exception error at run time
      import java.io.File;
      import com.db4o.Db4o;
      import com.db4o.ObjectContainer;
      import com.db4o.ObjectSet;
      import java.util.*;
           
      
           
      
                
           class Util {
      
          public final static String DB4OFILENAME="formula2.db4o";
      
          public static void listResult(ObjectSet result) {
              System.out.println(result.size());
              while(result.hasNext()) {
                  System.out.println(result.next());
              }
          }
          
          public static void listResult(java.util.List result){
               System.out.println(result.size());
               for(int x = 0; x < result.size(); x++)
                    System.out.println(result.get(x));
          }
          
          public static void listRefreshedResult(ObjectContainer 
      
      container,ObjectSet result,int depth) {
              System.out.println(result.size());
              while(result.hasNext()) {
                  Object obj = result.next();
                  container.ext().refresh(obj, depth);
                  System.out.println(obj);
              }
          }
          
          public static void retrieveAll(ObjectContainer db){
              ObjectSet result=db.get(new Object());
              listResult(result);
          }
          
          public static void deleteAll(ObjectContainer db) {
              ObjectSet result=db.get(new Object());
              while(result.hasNext()) {
                  db.delete(result.next());
              }
          }
      }
      
           
      
            class linked extends Util{
           public static void main(String args[]){
           ObjectContainer db=Db4o.openFile(Util.DB4OFILENAME);
            try {
                 LinkedList ll = new LinkedList();
                ll.add("a");
                ll.add("b");
                ll.add("c");
                db.set(ll);
                System.out.println("Stored linked list is "+ll);
      
           
      
      
           LinkedList proto = new LinkedList();
           ObjectSet result = db.get(proto);
           
           LinkedList ll2 = new LinkedList();
           while(result.hasNext()) {
           ll2.add(result.next());
           }
           /*Object[] s = ll2.toArray();
           String[] str = new String[ll2.size()];
           for(int m=0;m<s.length;m++)
           {
            str[m] = (String)s[m]; 
           System.out.println("str array" + str[m]);
           }*/
            String[] str = new String[ll2.size()];
           Iterator itr=ll2.iterator();
           for (int i=0, n=ll2.size(); i < n; i++)
           {
             Object o = itr.next();
           str[i] = (String)o;
           //System.out.println(o);
           System.out.println("str" +str);
      }

           
           

                }
           finally {
           db.close();
                }
      }}
      the error is
      C:\Documents and Settings\Sumit\Desktop\In Use\Practice\a>java linked
      Stored linked list is [a, b, c]
      Exception in thread "main" java.lang.ClassCastException: java.util.LinkedList
              at linked.main(linked.java:86)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        • 1. Re: class cast exception error
          3004
          At line 86 you're trying to cast a LinkeList to some type that it is not. I don't know which line is 86, and I don't know what you're trying to do, so I can't offer any specific suggestion on what to do instead.
          • 2. Re: class cast exception error
            807601
            as explained in your previous thread (http://forum.java.sun.com/thread.jspa?threadID=5250051), you are trying to cast a LinkedList into a String, which doesn't work.
            LinkedList proto = new LinkedList();
            ObjectSet result = db.get(proto);
            here you asked to get the set of all the LinkedList's in "db", so "result" is an ObjectSet<LinkedList>
            ll2.add(result.next());
            here, "result.next()" returns a LinkedList, which you then add to "ll2", which is a LinkedList<LinkedList> (linked list of linked lists)
            Iterator itr=ll2.iterator();
            here "itr" is a Iterator<LinkedList>
            Object o = itr.next();
            str[i] = (String)o;
            here, "o" is a LinkedList; hence the error when you cast it into String