Frage

I am trying to convert an expression from infix to postfix. I've tried debugging the code but I keep getting null pointer exception when the variable is popped. The input for which I get the error is :

(=> (NOT (award)) (badgrade))

The output after I get the error is (NOT award)

Please tell me if need to edit the Q to post lesser code/ add more comments/ provide any 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 a = 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 AND 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 the 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 are 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");
}

}

War es hilfreich?

Lösung

I was trying to access an element which was null at variable.peek(). removing all peek conditions, will make the program work.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top