0

I am trying to use this class to evaluate postfix expressions and when testing it am thrown this exception at line 32 where the virtual stack should push.

public class PostfixEval
{
  private IntStack s;

  public PostfixEval()
  {
    IntStack s = new IntStack();
  }

  public boolean isInteger(String s)
  {
    int i = 0;
    boolean isDigit = true;

    while(i < s.length() && isDigit)
    {
      isDigit = s.charAt(i) >= '0' && s.charAt(i) <= '9';
      i++;
    }
    return isDigit;
  }

  public int eval(String e)
  {
    String[] tokens = e.split("\\s+");


    for(int i=0; i<tokens.length; i++)
    {
      if(isInteger(tokens[i]))
      {
        s.push(Integer.parseInt(tokens[i]));
      }
      else 
      {
        int a,b,c;

        b = s.pop();
        a = s.pop();
        c = 0;

        char d = tokens[i].charAt(0);
        if(d == '+')
        {
          c = a + b;
        }
        else if(d == '-')
        {
          c = a - b;
        }
        else if(d == '*')
        {
          c = a*b;
        }
        else if(d == '/')
        {
          c = a/b;
        }
        else if(d == '%')
        {
          c = a%b;
        }
        else
        {
          System.out.println("Error");
          System.exit(0);
        }
        s.push(c);

      }
    }
  return s.peek();  

  }
}

I have used jgrasp to see what Integer.parseInt(tokens[i])) evaluates to and confirm it is a number from the split string. When trying to push a number that I type into the paramater of the push method it works, so why do I get null exception when using the PostfixEval to push?

Here is my stack class.

public class IntStack implements StackIntADT
{
  // fields
  private int[] stk;
  private int sp;
  // constructors
  public IntStack()
  {
    sp = -1;
    stk = new int[10];
  }

  public IntStack( int s )
  {
    sp = -1;
    stk = new int[s];
  }

  // stack class methods

  public void push(int element)
  {
    if(!isFull())
    {
      sp++;
      stk[sp]=element;
    }
    else
    {
      System.out.println("Element" + element);
      System.exit(0);
    }

  }
  public int pop()
  {
    int rv = 0;
    if(!isEmpty())
    {
      rv = stk[sp--];
    }
    else
    {
      System.out.println(rv);
      System.exit(0);
    }
    return rv;
  }
  public int peek()
  {
    return stk[sp];
  }
  public boolean isEmpty()
  {
    return sp==-1;
  }
  public boolean isFull()
  {
    return sp==stk.length-1;
  }
  public int size()
  {
    return stk.length;
  }
  public String toString()
  {
    String s = "";
    for(int x=0;x<10;x++)
    {
      s = s + " " + stk[x];
    }
  return s;
  }
}
1
  • You should delete IntStack before the s in PostfixEval() constructor.try this s = new IntStack(); Commented Mar 29, 2014 at 1:20

1 Answer 1

2

The constructor should not define a local s variable (which hides the member variable with the same name). The member variable is never assigned a value.

Change the constructor to this:

public PostfixEval() {
    s = new IntStack();
}
Sign up to request clarification or add additional context in comments.

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.