0

I have a set class shown below that extends to my setinterface class which just has some basic methods(add, remove, contains, etc). In my set class, i use HashSet library to create my type T parameters.

Using a Junit test on my setclass, i immediately run into a problem with my add method. I realized that my set add method doesn't at all add the object into my hashset when called upon. So my question is how do i call on the add method in hashset in my set add method so i can add an object to my hashset?

When i run a debugger, it shows that 100 is not being added to my set object. My set object does not contain 100 and is still null.

public class Set<T> implements SetInterface<T>{

    /*
     * i used hashset library because one, it does not
     * allow for duplicates, and 2 it allows my class to use
     * iterator 
     */
    private  HashSet<T> genericType;



    public Set() {


    }
    public Set(HashSet<T> genericType) {
        super();
        this.genericType = genericType;

    }
    /**
     * Add an item of type T to the interface  Duplicate items will not be
     * added to the set.
     * @param  itemToAdd - what to add.
     */
    @Override
    public void add(T itemToAdd) {

        genericType.add(itemToAdd); 

    }

heres my junit test if anyone wants to see it

public class SetTest {

    @Test
    public void testSet() {
        fail("Not yet implemented");
    }

    @Test
    public void testAdd() {
        Set<Integer> test = new Set<Integer>();
        test.add(100);

        assertTrue(test.contains(100));
    }
bpham93
  • 1
  • 3
  • 1
    You seem to already be doing what you want; this isn't being recursive. – Louis Wasserman Dec 02 '16 at 22:54
  • 1
    *I realized that my set add method just kind of recursively call itself when the add method is called on*: no, it doesn't. Why do you think that? What do you expect your test to do, and what does it do instead, precisely? Your test will just fail with a NullPointerException, since you initialize the hashset to null, and then try to add something to it. – JB Nizet Dec 02 '16 at 22:54
  • I bet your code will run into some NullPointerException instead of StackOverflowError. – glee8e Dec 02 '16 at 22:56
  • maybe recursively was the wrong word to use BUT what i meant was that when i call on add method in my SetTest class, it doesnt actually add the object to my hashset. – bpham93 Dec 02 '16 at 22:57
  • Say what it does instead! Post the error. It's meant to be read, not ignored. – JB Nizet Dec 02 '16 at 23:00
  • my apology, the error s like glee8e said, NullPointerException – bpham93 Dec 02 '16 at 23:05
  • I also deleted the code when i initialized my hashset to null but still has the same error. – bpham93 Dec 02 '16 at 23:06
  • That's what happens when you call a method on a variable initialized to null. Initialize it to a non-null HashSet. Not initializing it at all implicitly initializes it to null. – JB Nizet Dec 02 '16 at 23:06
  • oh okay thanks! i got it. a stupid mistake on my part – bpham93 Dec 02 '16 at 23:17

0 Answers0