0

so I am having a problem with null pointer exceptions and can't figure out what is wrong: so our homework task was to implement our own version of a stack by extending vector. This is what I got:

import java.util.Vector;
class Stack<T> extends Vector<T> {
    private Vector<T> stack;


    // returns false or true, given the stack is empty or not.
    public boolean isEmpty() {
        return stack.size() == 0;
    }

    //returns the top element of the stack without removing it.
    public T peek() {
        return stack.get(stack.size()-1);
    }

    //puts a new element to the top of the stack
    public void push(T element) {
        stack.add(element);
    }

    //returns and removes the top element of the stack
    public T pop() {
        return stack.remove(stack.size()-1);
    }
}

Then our second task was to find a way to implement a calculator that uses this stack. Here, I got this:

public class CalculatorImpl {
    //creating a new Integer Stack called calc
    private Stack<Integer> calc;

    //create a new calculator (Stack)
    CalculatorImpl(){
        calc = new Stack<>();
    }

    //sets the given integer
    public void set(int num){
        calc.push(num);
    }

    // adds the given integer
    public void add(int num){
        calc.push(num);
    }

    //subtracts the given integer.
    public void subtract(int num){
        calc.push(-num);
    }

    //multiplies by the given integer
    public void multiply(int num){
        int prev = calc.pop();
        calc.push(prev*num);
    }

    //clears what is entered previously
    public void clear(){
        calc.pop();
    }

    //divides by the given integer (integer division!)
    public void divide(int num){
        int prev = calc.pop();
        calc.push(prev/num);
    }

    //computes and returns the result of the expression.
    public int compute(){
        int out = 0;
        while (!calc.isEmpty()){
            out += calc.pop();
        }
        return out;

    }

}

It looks fine so far but as soon as I try to run the main method, a null pointer exception is thrown and I dont know why. This is the main method:

 class CalculatorApplication {
        public static void main(String[] args) {
            CalculatorImpl calculator = new CalculatorImpl();
            calculator.set(4);
            calculator.add(2);
            calculator.multiply(4);
            // 4 + 2 * 4 = 12
            System.out.println(calculator.compute());
    
            calculator.set(2);
            calculator.subtract(6);
            calculator.add(7);
            calculator.multiply(3);
            calculator.divide(2);
            calculator.add(5);
            // 2 - 6 + 7 * 3 / 2 + 5 = 11
            System.out.println(calculator.compute());
    
        }
    }

The error message is:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Vector.add(Object)" because "this.stack" is null
    at Stack.push(Stack.java:20)
    at CalculatorImpl.set(CalculatorImpl.java:12)
    at CalculatorApplication.main(Calculator.java:4)

Does anybody have an idea what I am doing wrong? Thanks for your tips!

Lari
  • 31
  • 4
  • 1
    You need to initialize the stack in the Stack class – Marlon May 19 '21 at 07:41
  • Thanks! I tried Stack stac = new Stack(); but this causes a stack overflow. Do you know what is wrong with the way I try to initialize it? – Lari May 19 '21 at 08:00
  • You seem to be confusing aggregation with inheritance. Your Stack type extends Vector and also contains a Vector. You should probably pick one or the other. (Better yet, don't use Vector.) – khelwood May 19 '21 at 08:16
  • Ohh yeah I didn't really know the difference. Our task was to extend vector (or anything else) to create a stack. But thanks, I'll have a look at that. – Lari May 19 '21 at 09:03

0 Answers0