Results 1 to 5 of 5
Like Tree2Likes
  • 2 Post By SurfMan

Thread: NullPointerException

  1. #1
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default NullPointerException

    From the complete reference 8th edition:

    A NullPointerException is thrown if an attempt is made to use a null object and null is not allowed in the set
    But I see I am able to add a null and even able to print null(see code below)

    Java Code:
    import java.util.*; 
    public class Illlegal {
    	public static void main(String args[])
    	{
    		Set s1=new HashSet();
    		s1.add(null);
    		System.out.println(s1);
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default Re: NullPointerException

    Only if the Set implementation doesn't allow null values a NPE is thrown. So a HashSet does allow null values to be entered to the Set; that's all the reference claims ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: NullPointerException

    Ok, so if I want to implement a set which does not allow null values. How do I do that?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: NullPointerException

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    Ok, so if I want to implement a set which does not allow null values. How do I do that?
    option 1) Find a Set implementation that does in fact not allow null-values. Set (Java Platform SE 7 )
    option 2) extend HashSet in your own class and override the add() and addAll() methods so you can add a check for a null value. Example:

    Java Code:
    public void add(E e){
      if(e == null){
         throw new NullPointerException("You can shove it!");
      }
    
      super.add(e);
    }
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,028
    Rep Power
    4

    Default Re: NullPointerException

    Quote Originally Posted by gimbal2 View Post
    option 2) extend HashSet in your own class and override the add() and addAll() methods so you can add a check for a null value. Example:
    Years from now, we will see stacktraces like this popping up on the interwebs :)
    Java Code:
    Exception in thread "main" java.lang.NullPointerException: You can shove it!
    Caused by: java.lang.NullPointerException: com.acme.NoNullHashSet.add(NoNullHashSet:42)
       at ...
    PhHein and DarrylBurke like this.

Similar Threads

  1. Why am I getting a NullPointerException?
    By atac57 in forum New To Java
    Replies: 7
    Last Post: 07-28-2012, 09:40 AM
  2. NullPointerException
    By Stormageddon in forum New To Java
    Replies: 3
    Last Post: 07-27-2012, 03:52 AM
  3. NullPointerException
    By Mate de Vita in forum New To Java
    Replies: 52
    Last Post: 03-22-2012, 12:27 PM
  4. NullPointerException help
    By laichunkitjacky in forum New To Java
    Replies: 4
    Last Post: 12-04-2011, 06:20 PM
  5. NullPointerException
    By donchini in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 02:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •