0

I'm trying to make a infix to post fix converter but the problem is that I keep getting null pointer excpetion with input like 3+1 or just (3+1) but I dont really understand what is wrong with the code.

line 26 and 38 are causing the NPE I used the debugger and i know that at that point the stack and queue isnt fillled with anything so they are both null but can i not use =! if it's pointing to null Can you not use something like ops.top.data!="(" if theres a possibility that the top of the stack(ops) is null. heres the code below. I have a seperate stack class that has a top node and queue class with front and rear node.

import java.util.StringTokenizer;
import acm.program.ConsoleProgram;

public class In2pJ extends ConsoleProgram{

    public void run() {
        //fields, 2 queue for input and output, 1 stack for operators, to keep find precedence, use index of strings and divide by 2
        Queue input = new Queue();
        Queue output = new Queue();
        Stack ops = new Stack();
        String oppre = "-+/*^"; //index divide by 2 create precedence of 0,0,1,1,2
        String paren = "()";
        println("Infix to Postfix Converter");
        String str = readLine("Enter string(blank line to exit):");
        StringTokenizer st = new StringTokenizer(str, "+-*/()", true);
        while(st.hasMoreTokens()) {

            input.enqueue(st.nextToken());//put tokens into input queue
        }

        while(true) {
            String cur=input.dequeue(); //get a token from input
            if(oppre.contains(cur)) {//if token is operator
                //if operator at top of stack has greater precedence or equal but the token is left associative, and the operator at top of stack is not a (
                //pop operators from stack to output queue
                while((oppre.indexOf(ops.top.data)>oppre.indexOf(cur)||(oppre.indexOf(ops.top.data)==oppre.indexOf(cur)&& cur!= "^"))&&ops.top.data!="("){
                    output.enqueue(ops.pop());

                }
                ops.push(cur);


            }else if(paren.contains(cur)) {//if token is  ()
                if (cur =="(") {//if ( push it onto stack
                    ops.push(cur);
                }else {//if )
                    //pop operator from the stack onto output if top of stack is not a left paren
                    while(ops.top.data!="(") {
                        output.enqueue(ops.pop());
                    }
                    ops.pop();//discard left paren when found
                }
            }else if(input.front==null &&input.rear==null) { //if input is empty pop operator from stack to output queue
                while(ops.top != null) {
                    output.enqueue(ops.pop());
                }
                break;
            }else {//if its a number push it to output queue
                output.enqueue(cur);
            }
        }
        print("Postfix:");
        while(output.rear!=null) {

            print(output.dequeue());
        }
    }


}
  • Which line is causing your NPE? – matt Mar 07 '20 at 21:16
  • 1
    this kind of question usually gets closed as a duplicate of "what is a null pointer exception and how do I solve it?" The general answer is "please learn how to use a debugger". – Mike Nakis Mar 07 '20 at 21:17
  • line 26 and 38 are causing the NPE I used the debugger and i know that at that point the stack and queue isnt fillled with anything so they are both null but can i not use =! if it's pointing to null – cantcode22 Mar 07 '20 at 21:29
  • 1
    No, you cannot use `ops.top.data!="("` if `ops.top` is null - you cannot access the `data` field of a null reference. And you also should not compare strings using `==` or `!=` -- this only compares string references, it doesn't check whether two strings contain the same characters. – Thomas Kläger Mar 07 '20 at 21:31

0 Answers0