-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Check if List is empty without causing an exception.
PostPosted: Fri Jan 08, 2010 7:19 pm 
Newbie

Joined: Fri Oct 05, 2007 7:35 am
Posts: 8
Hi all,

I have a problem with getting size of a list.
I have two objects: Game and Team. The object Game has a List of Team objects.
The first time I add a Game I did not add a Team object. When I go back to my overview page I get an Exception because I call teams.size().
How can I check if the List is not filled without causing an Exception?
I tried is teams == null but that did not worked.

With kind regards,

Rory


Top
 Profile  
 
 Post subject: Re: Check if List is empty without causing an exception.
PostPosted: Sat Jan 09, 2010 6:33 am 
Beginner
Beginner

Joined: Thu Mar 27, 2008 8:49 am
Posts: 27
Do you mean you tried:

Code:
if(teams != null) System.out.println("teams size is [" + teams.size() + "]");


which should work fine. However, if you tried:

Code:
if(teams == null) System.out.println("teams size is [" + teams.size() + "]");


then you would get a null pointer when you called teams.size();

It's not really a Hibernate question either so it may have been better to post it in a general Java forum rather than a specialist one but I'll help if I can.


Top
 Profile  
 
 Post subject: Re: Check if List is empty without causing an exception.
PostPosted: Sat Jan 09, 2010 7:25 am 
Newbie

Joined: Fri Oct 05, 2007 7:35 am
Posts: 8
I already fixed it. The problem was Lazy fetching I changed it to eager fetching.


Top
 Profile  
 
 Post subject: Re: Check if List is empty without causing an exception.
PostPosted: Sat Jan 09, 2010 8:20 am 
Beginner
Beginner

Joined: Thu Mar 27, 2008 8:49 am
Posts: 27
I thought my answer to your question was probably way too simple - my apologies. Glad you fixed it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.