0

I cant figure out why the minCheck function in the MinStack class does not successfully update the value of minVal when it is called from the push function. Ignore logical error that if minVal is popped, minVal's value will not represent the actual min value.

public class MinStack {
    
    int[] array; //array to hold elements
    int n = 0; // number of elements in array
    int minVal=10000; // smallest value in array (initialize to 1000 to prove it wont change)
    
    public int size() { //ignore this useless method
        return n;
    }
    
    
    public void push(int x) {
        if((n+1)>array.length)
            resize();
        array[n] = x;
        n++;
        minCheck(x); // minCheck will check if x is the new smallest value, and set minVal = x if it is.
    }
    
    public int pop() {
        int x = array[n-1];
        n--;
        return x;
    }

    public void minCheck(int x) {
        if(n==1) 
            minVal = x; //if x is the only element in the array, it must be the minVal 
        else {
            if(minVal>x) //if x is smaller than current minVal, it becomes the minVal
                minVal = x;
        }
    }

    private void resize() { //method not relevant to the question
        int[] secondArray = new int[(Math.max(1, n * 2))];
        for(int i=0; i<n; i++) {
            secondArray[i] = array[i];
        }
        array = secondArray;    
    }
    
    public String printMinVal() {
        return "minimum value is: " + minVal;
    }
    
    
    
    public static void main(String[] args) {
        MinStack myStack = new MinStack();
        
        
        try {
        myStack.push(3);
        myStack.push(2);
        myStack.push(1);
        myStack.push(4);
        myStack.push(5);
        myStack.pop();
        } catch(NullPointerException e) {};
        System.out.println(myStack.printMinVal()); //would expect to get "minimum value is 1" as console output, but instead get "minimum value is 1000"
                        
    }
}
stdunbar
  • 16,263
  • 11
  • 31
  • 53
  • **NEVER** catch a `NullPointerException`! And **NEVER** catch an exception and do nothing with it, not even log it. – luk2302 Jul 03 '20 at 19:41

1 Answers1

2

What is happening is that you are getting a NullPointerException in the first push(), since you didn't initialize the array.

Here:

public void push(int x) {
    if((n+1)>array.length) //array is null, calling length throws the exception
        resize();
    array[n] = x;
    n++;
    minCheck(x); // minCheck will check if x is the new smallest value, and set minVal = x if it is.
}

You catch the exception, but don't show it. So you believe it ran, but it really didn't. Initialize the array and everything should work properly.

As a suggestion, make sure you show all catched exceptions (via logging, or just print it to console), and/or don't continue the process after it, if it is a non-transient error, such as this one. It is dangerous not doing so..

aran
  • 10,978
  • 5
  • 39
  • 69