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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

ObjectSet to ArrayList

  • 27-04-2005 11:11pm
    #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