0

so I have to convert infix to postfix with the example, ab/(c-a)+de. My code did not show any errors only some warnings, it compiled but there is no output. When I run it will show me the message below.

Exception in thread "main" java.lang.NullPointerException
    at LinkedStackTest$Convert.convertToPostfix(LinkedStackTest.java:40)
    at LinkedStackTest$Convert.main(LinkedStackTest.java:60)

I tried looking over the code multiple times and still do not know what I did wrong. Any help or advice would be great thanks!

My code:

public class LinkedStackTest
{
    static class Convert
    {
        static int getPriority(char ch)
        {
            switch (ch)
            {
                case '+':
                case '-':
                    return 1;
                case '*':
                case '/':
                    return 2;
            }
            return -1;
        }

        public static String convertToPostfix(String infix)
        {
            LinkedStack<Character> linkStack = new LinkedStack<>();
            String postfix = "";

            for (int i = 0; i < infix.length(); i++)
            {
                char ch = infix.charAt(i);

                if (Character.isLetterOrDigit(ch)) {
                    postfix += ch;
                } else if (ch == '(') {
                    linkStack.push(ch);
                } else if (ch == ')') {
                    while (linkStack.peek() != '(') {
                        postfix += linkStack.peek();
                        linkStack.pop();
                    }
                } else {
                    while (!linkStack.isEmpty() && getPriority(ch) <= getPriority(linkStack.peek())) {
                        if (linkStack.peek() == '(')
                            return "Invalid";
                        postfix += linkStack.peek();
                    }
                    linkStack.push(ch);
                }
            }
            while (!linkStack.isEmpty())
            {
                if(linkStack.peek() == '(')
                    return "Invalid Expression";
                postfix += linkStack.pop();
            }
            return postfix;
        }

        public static void main(String[] args) throws Exception
        {
            String infix = "a*b/(c-a)+d*e";
            String postfix = convertToPostfix(infix);
            System.out.println("Infix: " + infix);
            System.out.println("Postfix: " + postfix);
        }
    }
}

Curiosa Globunznik
  • 3,129
  • 1
  • 16
  • 24
code_noob
  • 21
  • 5
  • 2
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Curiosa Globunznik Oct 26 '20 at 21:46
  • 1
    If looking over your code didn't provide any answers to what is wrong, then you should step through your code with a debugger. Follow the values of variables and the logic of your code and see where actual values differ from expected values. – Jonny Henly Oct 26 '20 at 22:03
  • which line is line 40? – Chris Gong Oct 26 '20 at 22:07
  • line 40 is : while(!linkStack.isEmpty() && getPriority(ch) <= getPriority(linkStack.peek()). Also I will try the debugger – code_noob Oct 26 '20 at 22:12
  • also line 60 is : when I assign postfix string to converToPostfix(infix) – code_noob Oct 26 '20 at 22:15
  • And are you using a custom implemented `LinkStack` class? – Chris Gong Oct 26 '20 at 22:17
  • I think so(?). I have a java class named LinkedStack, it includes nodes, checkIntegrity, checkCapacity, ensureCapacity, pop, push, peek, isEmpty, and clear. i don't know what you mean by custom. – code_noob Oct 26 '20 at 22:21
  • I figured it out, it was LinkedStack class that I used had an error with the nodes. Thanks for your question @Chris Gong, I was able to see my problem. – code_noob Oct 26 '20 at 23:38

0 Answers0