Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Null Pointer Exception--need help tonight!

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Null Pointer Exception--need help tonight!

    Hey there so I'm basically working on a bonus project for class and I'm having trouble with this null pointer exception. I'm supposed to create a class called SimpleSet that implements the dictionary interface but only uses keys, not values. The instructor gave us a jar file that contains all required classes to run SimpleSet. Anyway the method thats throwing the exception is the add method. Here's the code for the class:
    import java.util.*; 
     
    public class SimpleSet<K extends Comparable<? super K>> 
    { 
     
    /* use SimpleSortedDictionary object to hold set items  
         Note: Value is not needed, just set it to Boolean       */ 
    private DictionaryInterface<K, Boolean> items; 
     
    /* default constructor */ 
    public SimpleSet() 
    {
    	DictionaryInterface<K, Boolean> items = new SimpleSortedDictionary();
     
    }
     
    /* Adds a given object to the set. If the given object already exists in the set, 
        return false; otherwise, return true */ 
    public boolean add(K item)  //NOTE: this calls the dictionary interface method: public V add(K key, V value); 
     
    {
    	boolean result;
    	if(items.add(item, true) != null) 
    	{
    		result = true;
    	}
    	else result = false;
    	return result;
    }
     
    /* Removes a given object from the set. */ 
    public void remove(K item) 
    {
    	items.remove(item);
    }
     
    /* Sees whether the set contains a given object. */ 
    public boolean contains(K item) 
    {
    	return items.contains(item);
    }
     
    /* Clears all objects from the set. */ 
    public void clear()
    {
    	items.clear();
    }
     
    /* Gets the number of objects in the set. */ 
    public int getSize() 
    {
    	return items.getSize();
    }
     
    /* Returns an iterator to the set. */ 
    public Iterator<K> getIterator() 
    {
    	return items.getKeyIterator();
    }
     
    /* Returns a set that combines the items in two sets 
        Note: the union of current ("this") set and input otherset.  */ 
    public SimpleSet<K> union(SimpleSet<K> otherSet) 
    {
    	return null;
    }
     
    /* Returns a set of those items that occur in both of two sets (the intersection). 
             Note: the intersection of current ("this") set and input otherset.  */ 
    public SimpleSet<K> intersection(SimpleSet<K> otherSet) 
    {
    	return null;
    }
     
    /* Display objects */ 
    public String toString() 
    {
    	return "Set print out: "+this;
    }
     
    /* may define tests here */ 
    public static void main(String args[]) 
    {
    	SimpleSet setA = new SimpleSet();
    	Integer five = new Integer(5);
    	setA.add(five);
    	setA.toString();
     
    }
    } // end SimpleSet
    error message:

    Exception in thread "main" java.lang.NullPointerException
    at SimpleSet.add(SimpleSet.java:22)
    at SimpleSet.main(SimpleSet.java:83)
    Last edited by helloworld922; December 13th, 2010 at 10:11 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Null Pointer Exception--need help tonight!

    You declare a variable named items in your constructor that is NOT the same as the class variable named items. Then you attempt to use the uninitialized items variable in your add method, which causes the NPE.

    When posting code, use the code tags. Take a look at any other post as an example.

    How To Ask Questions The Smart Way

Similar Threads

  1. Null Pointer Exception Help!!
    By puzzledstudent in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 11th, 2010, 06:46 PM
  2. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  3. HttpURLConnection null pointer exception
    By chopficaro in forum Java Theory & Questions
    Replies: 0
    Last Post: May 10th, 2010, 10:19 AM
  4. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM