stack lifo

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    stack lifo

    Code:
    @Override
    public IAbstractQueryResult pop () { return lifo.pop();
    }
    In main

    Code:
    IAbstractQueryResult resLeft = qres.pop()
    here i get empty stack exception

    Thank YOU
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You are trying to get an element from an empty stack.

    Either your logic is wrong and pop is being called when it's not supposed to be called or you should check to see if there is an element before popping.

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      Code:
      QResStack qres = new QResStack();
      qres.push(new IntegerResult(1)); // (1)
      qres.push(new IntegerResult(2)); // (2)
      { // (3)
      IAbstractQueryResult resRight = qres.pop();
      IAbstractQueryResult resLeft = qres.pop();
      //iloczyn kartezjanski - operator przecinka
      IBagResult commaRes = QExecUtils.cartesianProduct(resLeft,resRight);
      qres.push(commaRes);
      }
      qres.push(new IntegerResult(3)); // (4)
      qres.push(new IntegerResult(4)); // (5)
      { // (6)
      IntegerResult resRight = (IntegerResult) qres.pop();
      IntegerResult resLeft = (IntegerResult) qres.pop();
      IntegerResult plusRes = new IntegerResult((Integer)resLeft.getValue() + (Integer)resRight.getValue());
      qres.push(plusRes);
      }
      { // (7)
      IAbstractQueryResult resRight = qres.pop();
      IAbstractQueryResult resLeft = qres.pop();
      at the last line i get
      run:
      Exception in thread "main" java.util.Empty StackException
      at java.util.Stack .peek(Stack.jav a:102)
      at java.util.Stack .pop(Stack.java :84)
      at s4409_s4409_pr2 .QResStack.pop( QResStack.java: 20)
      at s4409_s4409_pr2 .S4409_s4409_pr 2.main(S4409_s4 409_pr2.java:51 )
      Java Result: 1

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Without knowing the exact implementation of your stack it's difficult to tell; I implemented a simple stack, used it with your code and it works fine. Maybe debug the QResStack function? It seems that whatever internal data collection you're using there's no element in it at the last line.

        Comment

        Working...