0

I am having issues with my code regarding exception in thread main. This is the error that is popping up

Exception in thread "main" java.lang.IllegalStateException: Attempt to create a stack whose capacity exceeds allowed maximum of 10000 
    at ResizeableArrayStack.checkCapacity(ResizeableArrayStack.java:74)
    at ResizeableArrayStack.ensureCapacity(ResizeableArrayStack.java:82)
    at ResizeableArrayStack.push(ResizeableArrayStack.java:28)
    at ResizeableArrayStack.evaluatePostfix(ResizeableArrayStack.java:98)
    at ResizeableArrayStack.main(ResizeableArrayStack.java:157)

This is my code

import java.util.*;
public class ResizeableArrayStack<T> implements StackInterface<T> 
{
    private T[] stack;
    private int topIndex;
    private boolean integrityOK = false;
    private static final int DEFAULT_CAPACITY = 50;
    private static final int MAX_CAPACITY = 100000;
    
    public ResizeableArrayStack() 
    {
        this(DEFAULT_CAPACITY);
    }

    public ResizeableArrayStack(int initialCapacity) 
    {
        integrityOK = false;
        checkCapacity(initialCapacity);
        // The cast is safe because the new array contains null entries
        @SuppressWarnings("unchecked")
        T[] tempStack = (T[])new Object[initialCapacity];
        stack = tempStack;
        topIndex = -1;
        integrityOK = true;
    }
    
    public void push(T newEntry) {
        checkIntegrity();
        ensureCapacity();
        stack[topIndex + 1] = newEntry;
        topIndex++;
    }
    
    private void checkCapacity(int capacity) {
        if (capacity > MAX_CAPACITY) {
            throw new IllegalStateException("Attempt to create a stack whose capacity exceeds allowed 
            maximum of " + MAX_CAPACITY);
        }
    } // end checkCapacity
    
    private void ensureCapacity() {
        if (topIndex >= stack.length - 1) {
        // If array is full, double its size 
            int newLength = 2 * stack.length;
            checkCapacity(newLength);
            stack = Arrays.copyOf(stack, newLength);
        }
    } //end ensureCapacity
    
    public static void main(String[] args) {
        String input = "ab*ca-/de*+";
        ResizeableArrayStack<String> astack = new ResizeableArrayStack<>(input.length());
        int evaluation = astack.evaluatePostfix(input);
        System.out.println("Evaluating Postfix Expressions");
        System.out.println("The evaluation of the postfix expression is " + evaluation);
    }
}

I'm pretty sure the issue is with how the capacity values are set and compared but I can't figure out why I am getting this error. I think the issues are within the constructors that involve capacity and main method. Please ignore the evaluatePostfix method in the main as the errors all say they come from the constructors and main. I can put the evaluatePostfix up if you think the problem is within it. I also deleted the methods that weren't brought up in the problem.

Macrus
  • 1
  • 1
  • Refer to [How to read and understand the java stack trace?](https://stackoverflow.com/questions/12688068/how-to-read-and-understand-the-java-stack-trace) Line 74 of file `ResizeableArrayStack.java` is throwing the `IllegalStateException` – Abra Oct 28 '20 at 08:10
  • I cannot reproduce your error. I have no problem running this code (although I don't have StackInterface). – Milgo Oct 28 '20 at 08:11

1 Answers1

0

The code you posted is not a MCVE. It doesn't compile because it is missing methods including checkIntegrity and evaluatePostfix. Even after I work around the missing methods, the code you posted does not cause IllegalStateException to be thrown. At a guess, after looking through the code that you did post, as well as the stack trace, the culprit appears to be method ensureCapacity which contains the following line:

int newLength = 2 * stack.length;

The value assigned to newLength may be greater than MAX_CAPACITY.

After you assign a value to newLength, you call method checkCapacity which explicitly throws a IllegalStateException.

private void checkCapacity(int capacity) {
    if (capacity > MAX_CAPACITY) {
        throw new IllegalStateException("Attempt to create a stack whose capacity exceeds allowed maximum of " + MAX_CAPACITY);
    }
} // end checkCapacity

But as I wrote earlier, in the code that you posted capacity is never greater than MAX_CAPACITY and hence the code in your question never throws IllegalStateException.

I recommend that you run your code through a debugger. If you are using an IDE, then it should give you the option of running your code in "debug" mode. If you don't know how to use the debugger of the IDE then you should learn because knowing how to debug code is an essential skill for a programmer and a debugger helps a lot when it comes to debugging your code.

Abra
  • 19,142
  • 7
  • 29
  • 41