-2

Main idea of class is implement Integer subarray. For example, I would like to put numbers between 10 and 20 in array. If number is in array, there will be true inits place in array. Number 10 has index 0 in array, etc.

I get problem with my second constructor:

public FixedRangeIntegerSet(int lowerBound, int upperBound, Iterable<Integer> elements) {
    int sizeCopy = upperBound - lowerBound + 1;
    for (Integer element : elements) {
        if (element >= lowerBound && element <= upperBound)
            add(element);
    }
}

Here is rest of my code:

public class FixedRangeIntegerSet implements Iterable<Integer> {

    private boolean[] elementFlags;
    private int size;
    private int lowerBound;
    private int upperBound;

    public FixedRangeIntegerSet(int lowerBound, int upperBound) {
        this.lowerBound = lowerBound;
        this.upperBound = upperBound;
        int size = upperBound - lowerBound + 1;
        elementFlags = new boolean[size];
        this.size = size;
    }

    public FixedRangeIntegerSet(int lowerBound, int upperBound, Iterable<Integer> elements) {
        int sizeCopy = upperBound - lowerBound + 1;
        for (Integer element : elements) {
            if (element >= lowerBound && element <= upperBound)
                add(element);
        }
    }

    public FixedRangeIntegerSet(int lowerBound, int upperBound, Integer... elements) {
        for (int i = 0; i < elements.length; i++) {
            add(elements[i]);
        }
    }

    public boolean add(Integer element) {
        if (elementFlags[element - lowerBound] == true) {
            return false;
        }
        elementFlags[element - lowerBound] = true;
        return true;
    }

    public boolean containsElement(Integer element) {
        if (element < lowerBound || element > upperBound) {
            return false;
        }
        if (elementFlags[element - lowerBound] == true) {
            return true;
        } else
            return false;
    }

    public int getSize() {
        int size = 0;
        for (Boolean i : elementFlags) {
            if (i == true) {
                size++;
            }
        }
        return size;
    }

    public int getLowerBound() {
        return lowerBound;
    }

    public int getUpperBound() {
        return upperBound;
    }

    public boolean remove(Integer element) {
        if (elementFlags[element - lowerBound] == false) {
            return false;
        } else {
            elementFlags[element - lowerBound] = false;
            return true;
        }
    }

    @Override
    public Iterator<Integer> iterator() {
        return new RangeIter();
    }

    private class RangeIter implements Iterator<Integer> {

        private int currentIndex;

        public RangeIter() {
            this.currentIndex = 0;
        }

        @Override
        public boolean hasNext() {
            for (int i = currentIndex; i < elementFlags.length; i++) {
                if (elementFlags[i] == true)
                    return true;
            }
            return false;
        }

        @Override
        public Integer next() {

            if (!hasNext()) {
                throw new NoSuchElementException("No elements");
            }
            while (currentIndex < size - 1 && !elementFlags[currentIndex]) {
                currentIndex++;
            }
            return currentIndex++ + lowerBound;
        }
    }
}

This is pseudocode for main (test) method:

 FixedRangeIntegerSet s1 = new FixedRangeIntegerSet(10, 20);
            s1.add(14);
            s1.add(16);
            s1.add(18);
            System.out.println("Elements:");
    foreach loop;
FixedRangeIntegerSet s2 = new FixedRangeIntegerSet(10, 20, s1);
        s2.remove(16); // here is where I get error

After all, my question is: Why I get null pointer exception, or where I am wrong in writing second constructor in my code? Thanks!

user5507755
  • 251
  • 1
  • 9

1 Answers1

2

elementFlags is not initialised in the second constructor. In the add method, you're accessing it. But it's null, so you're getting the NullPointerException

Ayush Kumar
  • 148
  • 5
  • Can you write me line which I need to insert? Please – user5507755 Dec 03 '15 at 13:31
  • Is this homework? If it is, I'm afraid I can't help you. It'll be better if you try to understand why the NPE is occuring. – Ayush Kumar Dec 03 '15 at 13:33
  • No,it is not. I want to solve my problem – user5507755 Dec 03 '15 at 13:35
  • 1
    @user5507755: Nobody here is going to write all of your code for you. *Look* at your code and *listen* to what people here are telling you. You initialized the `elementFlags` variable in one constructor, but not in the other. Try initializing it in the other as well. – David Dec 03 '15 at 13:36
  • 1
    @user5507755 Please check out the link mentioned by Kevin Esche.. – Ayush Kumar Dec 03 '15 at 13:37
  • @user5507755 in addtion you could call `this(lowerBound, upperBound)` for the other constructors, since this seems to be some sort of base constructor that initializes the important variables. – SomeJavaGuy Dec 03 '15 at 13:38