简体   繁体   中英

Exception in thread “main” java.lang.NullPointerException virtual stack class

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;
  }
}

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM