-3

The assignment was to create a postfix to infix converter using Stacks. The program compiles properly but when I tried to make a demo class, I received a null point exception line 32. Please share any observations, better coding conventions, or solutions.

import java.util.Stack;

public class PostfixtoInfix {
    private String expression;
    private Stack<Character> s;
    Character pOpen = new Character('(');
    Character pClose = new Character(')');

    public String PostfixtoInfix(String e) {
        expression = e;
        String output = "";
        for (int i = 0; i < e.length(); i++) {
            char currentChar = e.charAt(i);
            if (isOperator(currentChar)) {
                while (!s.empty() && s.peek() != pOpen
                        && hasHigherPrecedence(s.peek(), currentChar)) {
                    output += s.peek();
                    s.pop();
                }
                s.push(currentChar);
            } else if (isOperand(currentChar)) {
                output += currentChar;
            } else if (currentChar == '(') {
                s.push(currentChar);
            } else if (currentChar == ')') {
                while (!s.empty() && s.peek() != pClose) {
                    output += s.peek();
                    s.pop();
                }
            }
            while (!s.empty()) {
                output += s.peek();
                s.pop();
            }
        }
        return output;
    }

    public boolean isOperator(char c) {
        if (c == '+' || c == '-' || c == '/' || c == '*' || c == '^')
            return true;
        return false;
    }

    public boolean isOperand(char c) {
        if (c >= '0' && c <= '9')
            return true;
        if (c >= 'a' && c <= 'z')
            return true;
        if (c >= 'A' && c <= 'Z')
            return true;
        return false;
    }

    public int getOperatorWeight(char operator) {
        int weight = -1;
        switch (operator) {
        case '+':
        case '-':
            weight = 1;
            break;

        case '*':
        case '/':
            weight = 2;
            break;

        case '^':
            weight = 3;
        }
        return weight;
    }

    public boolean hasHigherPrecedence(char operator1, char operator2) {
        int op1 = getOperatorWeight(operator1);
        int op2 = getOperatorWeight(operator2);
        if (op1 == op2) {
            if (isRightAssociative(operator1))
                return false;
            else
                return true;
        }
        return op1 > op2 ? true : false;
    }

    public boolean isRightAssociative(char op) {
        if (op == '^')
            return true;
        return false;
    }
}
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
  • Advice? Learn to use your debugger and google. – takendarkk Dec 08 '15 at 23:48
  • I voted to close because "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers." – bhspencer Dec 08 '15 at 23:49

4 Answers4

2

To fix the NPE initialize your objects. Unlike C++, Stack<Character> s; is equivalent to Stack<Character> s = null;; not to Stack<Character> s = new Stack<>();!

Beware of == and != not behaving as you might expect for boxed objects.

Character a = new Character('A');
Character aa = new Character('A');
System.out.println(a == aa);

gives the (correct!) answer false.

They are different objects. If you want to compare for equality, use either:

System.out.println(a.equals(aa));
System.out.println((char)a==(char)aa);

The first uses an explicit method for comparing the object contents. The second one avoids this problem by using non-object primitives, where equality is bitwise, not reference-equality.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
0

It appears that you declare a private member s, never assign anything to it, and then attempt to use it in expressions like s.empty() and s.pop(). If nothing is ever assigned to s, then it is null, and attempting to call a method on it will result in a NullPointerException.

To create an empty stack, you probably want to change the declaration to:

private Stack <Character> s = new Stack<Character>();
Dave Costa
  • 47,262
  • 8
  • 56
  • 72
0

First of all, you have a method looking like a constructor:

public String PostfixtoInfix(String e) {

try changing it to something else, like:

public String transform(String e) {

Second, your s field never gets assigned a stack. Put

s = new Stack<Character>();

in your constructor. Also, new Character('a') != new Character('a'), because that will bypass automatic (pillowweight cached) boxing. Use instead just simple chars as pOpen and pClose.

Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
-2

Your access modifier may be preventing the program to access the stack. Change:

private Stack <Character> s; 

to:

protected Stack <Character> s;

Read more here

Community
  • 1
  • 1
dphamcc
  • 17
  • 3
  • 3
    Don't know of any way that would cause a NullPointerException. Should cause a compiler error if anything. – Dave Costa Dec 08 '15 at 23:44