Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

ObjectSet to ArrayList

Options
  • 28-04-2005 12:11am
    #1
    Moderators, Music Moderators Posts: 23,361 Mod ✭✭✭✭


    Hopefully this isn't blatantly obvious and it's possible but I'm using DB4O to store objects in a file and to read, write and update those objects. I have an existing project that I have to make work with DB4O. When I run a query on the database, it returns an ObjectSet. However, the rest of my code requires an ArrayList to be returned. Is there any reasonably handy way to convert one to the other?


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Well, I'm not signing up just so I can check their documentation on ObjectSet, but is it anything like org.odbms.ObjectSet? If it is, the problem is that since it doesn't inherit anything, it isn't as easy to cast it. You'll probably have to loop through it.
    ObjectSet os = ~~
    ArrayList al = new ArrayList();
    
    while(os.hasNext())
    	al.add(os.next());
    


  • Moderators, Music Moderators Posts: 23,361 Mod ✭✭✭✭feylya


    I wrote a function to do that:
    public static ArrayList listResultToArray(ObjectSet result)
        {
        	ArrayList a = null;
            System.out.println(result.size());
            while(result.hasNext()) 
            {
            	System.out.println(result.next());
                a.add(result.next());
            }
            return a;
        }
    

    but any time I run it:
    java.lang.NullPointerException
    	at cp3036proj.Util.listResultToArray(Util.java:25)
    	at cp3036proj.dao.CategoryDao.getCategories(CategoryDao.java:43)
    	at cp3036proj.dao.CategoryDao.main(CategoryDao.java:77)
    

    There's something obvious there but I can't see it.


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Can't add anything to an ArrayList until you create one ;)


  • Moderators, Music Moderators Posts: 23,361 Mod ✭✭✭✭feylya


    Gah. Spot on. Cheers mate. That'll teach me for trying to code tired!


Advertisement