java - Conversion from prefix to infix: Null Pointer exception -


i trying convert expression infix postfix. i've tried debugging code keep getting null pointer exception when variable popped. input error :

(=> (not (award)) (badgrade))

the output after error (not award)

please tell me if need edit q post lesser code/ add more comments/ provide more information. thanks!

public class toinfix1 {  stack<string> symbol = new stack<string>(); stack<string> variable = new stack<string>(); stack<string> operator = new stack<string>(); static string inputfile = "kb2.txt"; arraylist<string> infix = new arraylist<string>();  public void toprefix1() {     try {         file f = new file(inputfile);         filereader fr = new filereader(f);         bufferedreader bf = new bufferedreader(fr);         string str;         string kb = "";          while ((str = bf.readline()) != null) {             pattern pattern = pattern.compile("[(]|[)]|<=>|=>|\\w+|^\\s+");             matcher m = pattern.matcher(str);             //int = 0;             system.out.println("kb" + kb);             while (m.find()) {                 string node1 = m.group();                 system.out.println("node1" + node1);                  //if  (                 if (node1.equals("(")) {                     symbol.push(node1);                  } else if (node1.equals("or") || node1.equals("and")                         || node1.equals("not") || node1.equals("=>")                         || node1.equals("<=>")) {                     operator.push(node1);                      //if )                 } else if (node1.equals(")")) {                     //pop symbol (                      if(!variable.empty()&& !operator.empty() && !symbol.empty()){                          string symbol1 = "", op = "";                         if (symbol.peek() != null && !symbol.empty()) {                             symbol1 = symbol.pop();                         }                         //pop if  operator , or => <=> (binary)                         if (operator.peek() != null && !operator.empty()) {                             op = operator.pop();                             if (op.equals("and") || op.equals("or")                                     || op.equals("=>") || op.equals("<=>")) {                                 string var2 = "";                                 string var1 = "";                                 if (variable.peek() != null && !variable.empty()) {                                     var1 = variable.pop();                                 }//error occurs in following if condition                                  if (variable.peek() != null && !variable.empty()) {                                     var2 = variable.pop();                                 }                                 kb = "(" + var1 + op + var2 + ")";                                 variable.push(kb);                                 //pop if operator not   (unary)                             } else if (op.equals("not")) {                                 string var1 = "";                                 if (variable.peek() != null && !variable.empty()) {                                     var1 = variable.pop();                                 }                                 kb = "(" + op + var1 + ")";                                 variable.push(kb);                                 //no operator after popping )                             }                          }                      }                                        //if there no operators                 }   else {                      variable.push(node1);                 }             }         }            fr.close();      } catch (exception e) {         system.err.println("error thrown" + e.getmessage());     } }  public static void main(string[] args) {     system.out.println("in new file");     toinfix1 inf1 =  new toinfix1();     inf1.toprefix1();     system.out.println("completed"); } 

}

i trying access element null @ variable.peek(). removing peek conditions, make program work.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -