0

Below is just one little chunk of my code, I am trying to understand why if I add in the line of code that is commented out it throws an EmptyStackException. I need to add the item at the top of oneStack to the top of twoStack if there are two null values in a row (generated by calling items.getNextItem()). Any insight into why this breaks? Or how I can get the top value of oneStack to also be the top value of twoStack at that point?

I have tried putting that line of code in an if, assigning a variable for the value of oneStack.peek(), but none of that has helped either. It is almost as if that one commented out line is emptying the whole stack (??).

Main point: if in that commented out line of code I swap oneStack.peek() with any other value, it works just fine. So why does it not work with oneStack.peek()?

oneStack.push(firstItem);
twoStack.push(firstItem);
nextItem = items.getNextItem();
oneStack.push(nextItem);
twoStack.push(nextItem);
while (!done) {
        if (oneStack.peek() == null) {
            oneStack.pop();
            oneStack.pop();
            twoStack.pop();
            twoStack.push(oneStack.peek()); // the commented out line below causes this line to throw an EmptyStackException if uncommented.
            newItem = items.getNextItem();
            oneStack.push(nextItem);
            if (oneStack.peek() == null) {
                oneStack.pop();
                twoStack.pop();
                //twoStack.push(oneStack.peek()); // if I uncomment this it breaks, but this needs to happen for twoStack to be correct
            } else {
                twoStack.push(nextItem);
            }
        } else if (oneStack.peek() == targetItem) {
            done = true;
        } else {
            nextItem = items.getNextItem();
            oneStack.push(nextItem);
            twoStack.push(nextItem);
        }

This is the way that items are being generated:

item1, item2, item3, item4, item5, null, null, item6, item7

In the end, this is what is left of the stacks: oneStack: item1, item2, item6, item7

twoStack: item1, item2, item3, item4, item5, item4, item6, item7 (item3 is missing)

twoStack should be: item1, item2, item3, item4, item5, item4, item3, item6, item7

11
  • 1
    you are popping from oneStack twice then you are peeking. How many items does this stack has? Commented Feb 7, 2017 at 1:39
  • @efekctive I need to pop from oneStack twice in order to remove the null value and the Item that is previous to it to start over with the item before that one (sorry for how confusing that sounds). Basically, if a null value is generated, it should be removed from oneStack and so should the previous item, and the item prior to that should be added to twoStack again. Commented Feb 7, 2017 at 1:42
  • Ok. but are you sure of the size of the stack at that point? From you code it could be empty or not. what is the size prior to the pushes? Commented Feb 7, 2017 at 1:45
  • @efekctive there are six items in the stack when it hits the null value that is popped... null and the previous item are popped off, leaving four items, and if it goes through the loop again then two more are popped off, still leaving two items :/ Commented Feb 7, 2017 at 1:52
  • The exception is quite clear. What is the size of the stack prior to the loop and after how many iterations the exception happens? Maybe add them to the post ( null, not null, null) in the order they would be popped Commented Feb 7, 2017 at 1:54

3 Answers 3

1

I recommend before attempting to pop anything off the stack or peek, you first check that it is not empty. At least it would prevent the exception.

Sign up to request clarification or add additional context in comments.

3 Comments

But preventing the exception without knowing why it happened is as bad. It just delays another crash
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
@Joseph: This seems like a rational reason for me. peek() will throw the EmptyStackException if nothing's in it.
0

Presumably your stack is empty after popping off the last thing, then peeking throws an exception because there is nothing in the stack left to peek.

1 Comment

I understand that, but the stack is quite full when this is happening, so it doesn't make sense to me why it is acting as if it is empty... adding an if around it hasn't helped either :/
0

Prior to loop:

oneStack = item1, item1 (if item1 is in items) or item2
twoStack = item1, item1 (if item1 is in items) or item2 let's assume item2

Size is two entering loop.

First, second, third, fourth times around:

oneStack = item1, item2 item3 item4 item5 null
twoStack = item1, item2 item3 item4 item5 null

Fifth time

oneStack = item1, item2 item3 item4 
twoStack = item1, item2 item3 item4 item5 item4

Sixth time

oneStack = item1, item2 item3 item4 null
twoStack = item1, item2 item3 item4 item5 item4 null

Seventh time around:

oneStack = item1, item2 item3 
twoStack = item1, item2 item3 item4 item5 item4 item3

eighth and nineth times around

oneStack = item1, item2 item3 item6 item7
twoStack = item1, item2 item3 item4 item5 item4 item3 item6 item7

As you can see it is unclear what the contents of items are at the beginning. I would print to console each pop and the whole stacks after each round. Also print the contents of the stacks and items before entering the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.