Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

testing for null elements in a set

Thread view: 
Murat Tasan - 18 Apr 2005 18:09 GMT
in a method i'm writing, i'd like to test for null elements in an
arbitrary set.
if the set contains a null element, i want to throw a NullPointerException.

rather than examining the elements myself, though i'd like to use the
Set.contains(Object) method.

here's the problem though:

in the java api it is possible for calling Set.contains(null) to throw a
NullPointerException if that set doesn't permit nulls in the first place.
(note, i doubt any implementation of Set actually does this, but i have to
follow the api.)

so, i can wrap my test in a try block:

try{
 if(myset.contains(null)) throw new NullPointerException("nulls not
 allowed...");
}
catch{NullPointerException e)
{
 // do something
}

the catch is needed to handle any Set objects that are passed that cannot
handle the contains(null) call.

but, i'd like to pass NullPointerException back up the call stack as is
done in the try block.

obviously though, if a null element is found, the catch block will catch
my NullPointerException that i want passed back up.

so, i have 2 solutions in mind:

1)  test each element one at a time manually using an iterator.

2)  create my own NullElementFoundException and handle it appropriately

but both of these solutions, while easy, are not particularly elegant.

anyone else ever have a situation like this with the java api and know of
a nice solution?

thanks
Eric Sosman - 18 Apr 2005 18:40 GMT
> in a method i'm writing, i'd like to test for null elements in an
> arbitrary set.
[quoted text clipped - 40 lines]
> anyone else ever have a situation like this with the java api and know of
> a nice solution?

   Solution #1:

    boolean hasNull = false;
    try {
       hasNull = myset.contains(null);
    }
    catch (NullPointerException ex) {
       // ignore: myset obviously has no nulls
    }
    if (hasNull)
       throw new NullPointerException (...);

   Solution #2: Use a different exception, one that describes
the situation better.  IllegalArgumentException, maybe, or
perhaps IllegalStateException.

   Solution #3: Must the Set really be "arbitrary," or can you
use a class of your own that implements Set?  If so, you could
throw IllegalArgumentException (or whatever) when a null is first
inserted, which could be a good deal more informative.

Signature

Eric.Sosman@sun.com

Murat Tasan - 18 Apr 2005 18:49 GMT
>> in a method i'm writing, i'd like to test for null elements in an
>> arbitrary set.
[quoted text clipped - 61 lines]
> throw IllegalArgumentException (or whatever) when a null is first
> inserted, which could be a good deal more informative.

thanks, i like your solution 1 better than my other alternatives.  as far
as using an arbitrary set, yes... as eventually my code will become part
of a very generic api.

thanks again.
Tony Morris - 19 Apr 2005 00:44 GMT
> in a method i'm writing, i'd like to test for null elements in an
> arbitrary set.
[quoted text clipped - 42 lines]
>
> thanks

Using a Set for something it was never intended for.
If all you have is a hammer, everything looks like a nail.
Admittedly, Java 2 Collections aren't very well constructed hammers.

Signature

Tony Morris

JTiger Unit Test Framework for J2SE 1.5
http://www.jtiger.org/
Java Q&A (FAQ, Trivia)
http://qa.jtiger.org/
http://xdweb.net/~dibblego/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.