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?