-4

It keeps showing a null pointer as well as a array out of bounds exception:

public class Intopost {
    int top = -1;

    public void intopost() {
        int i;
        Stack stack = new Stack(20);

        String s1 = "b*b-4*m*c";
        String s2 = " ";
        int top = -1;
        System.out.println("your infix string is" + s1);
        for (i = 0; i < s1.length(); i++) {

            if (s1.charAt(i) == '*' || s1.charAt(i) == '-') {
                if (top == -1) {
                    stack.push(s1.charAt(i));
                    top++;
                } else {
                    if (stack.peek() == '*' && s1.charAt(i) == '-') {

                        while (top != -1) {
                            s2 += stack.pop();
                        }
                        top = -1;
                    } else {

                    }
                    stack.push(s1.charAt(i));

                }

                if (i == (s1.length() - 1)) {
                    while (top != -1) {
                        s2 += stack.pop();
                    }
                    top = -1;

                }

            } else {
                s2 += s1.charAt(i);
                if (i == (s1.length() - 1)) {
                    while (top != -1) {
                        s2 += stack.pop();
                    }
                    top = -1;

                }

            }

        }
        System.out.print("the postfix string is" + s2);
    }

    public static void main(String args[]) throws IOException {
        Intopost in = new Intopost();

        in.intopost();

    }

    class Stack {
        int maxSize;
        char[] sa;

        public Stack(int max) {
            maxSize = max;
            sa = new char[maxSize];
            top = -1;
        }

        public void push(char a) {
            sa[++top] = a;
        }

        public char pop() {
            return sa[top--];

        }

        public char peek() {
            return sa[top];
        }

    }

}

Can you please tell me what could be the error here which keeps giving me the null exception error in this program?

Steffen
  • 3,999
  • 1
  • 23
  • 30
sshh
  • 139
  • 1
  • 8
  • Assign a value to the variable different to `null`... Spot where the variable takes its `null` value by using a debugger... – Luiggi Mendoza Apr 26 '14 at 17:07
  • 1
    You should inspect the line carefully that throws the NullPointerException, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Apr 26 '14 at 17:12
  • @LuiggiMendoza can you pls point out to tht line!!!i mean am really lost with these errors – sshh Apr 26 '14 at 17:13
  • If you read the stacktrace, you would spot the line... – Luiggi Mendoza Apr 26 '14 at 17:14
  • For cripes sake, you have the error message that is **telling** you the line. It is much better for you to read the error message, find the line that it is referring to in your code and then let **us** know. – Hovercraft Full Of Eels Apr 26 '14 at 17:14
  • Why are you implementing your own stack? The exception that was being thrown was `ArrayIndexOutOfBoundsException` with a -1 index not a NPE. – Jeff Ward Apr 26 '14 at 17:14
  • @JeffWard after i corrected the null pointer exception,i got the array out of bounds one !!it pointed at the method public char pop(){return sa[top--]} – sshh Apr 26 '14 at 17:16
  • @sshh Unless you are being required to implement your own stack for this infix to postfix converter you should be using the tested and reliable one provided for you http://docs.oracle.com/javase/7/docs/api/java/util/Stack.html – Jeff Ward Apr 26 '14 at 17:21
  • @HovercraftFullOfEels i just got the null pointer exception verified!!but the arrayoutofbounds is still confusing!! it is pointing out to the method, pop() in the class stack!! – sshh Apr 26 '14 at 17:22

2 Answers2

0

You have multiple problems with incrementing and decrementing top, which you are apparently using to see where you are on the stack. You also have top in the stack you've created, and it looks correct at there, but in your other code you are checking the local top which is never checked in places like:

while(top!=-1) {
    s2+=stack.pop();
}
michaelp
  • 353
  • 6
  • 24
0

I still don't understand why you are implementing your own stack. If you plan to continue to use your stack implementation it should behave like the Stack provide by the language and throw an exception if you call pop and the stack is empty (i.e. top is a negative value).

  public char pop() {
     if (top >= 0) {
        return sa[top--];
     }
     throw new EmptyStackException();
  }

I strongly encourage you to use the Stack class instead http://docs.oracle.com/javase/7/docs/api/java/util/Stack.html or at least implement yours to behave in a similar fashion.

Jeff Ward
  • 1,109
  • 6
  • 17
  • thank you sooo much!!!!i got it now!!this is the first time i am trying stack in my program!! and why is infix to postfix done only using stack!!cant it be done using string!!what i mean is,instead of storing the operators in a stack,cant i store them in a string!! – sshh Apr 26 '14 at 17:39
  • You can use a number of different approaches for converting infix to postfix. Using a string would require you to have even more logic for the formatting. Out of curiosity is this for an assignment? – Jeff Ward Apr 26 '14 at 17:43
  • okiee!!thnks!!no....not assignment!!am trying to learn java!! – sshh Apr 26 '14 at 17:49
  • @sshh if I answered your question sufficiently can you mark it as such? Also I would still recommend you using the `java.util` stack versus implementing your own if you are just attempting to learn the language. – Jeff Ward Apr 26 '14 at 17:51