Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

ObjectSet to ArrayList

  • 28-04-2005 12:11AM
    #1
    Moderators, Music Moderators Posts: 23,363 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,363 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,363 Mod ✭✭✭✭feylya


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


Advertisement