尝试在Java中运行PostFixCalculator时出现NullPointerException

时间:2014-04-01 22:17:12

标签: java list map compiler-errors

我已经让我的代码工作到可以编译和运行的程度,但现在我在两个不同的地方得到例外如下:

Exception in thread "main" java.lang.NullPointerException
at PostFixCalculator.storeOperand(PostFixCalculator.java:97)
at CalcTest.main(CalcTest.java:17)

......我不确定到底出了什么问题。第一个错误发生在

后面的代码中
myStack.push(operand);

但是我很遗憾到这里要去哪里......

    import java.util.*;
    public class PostFixCalculator {
    private DoubleStack<Double> myStack;
    private ArrayList<Double> evalList;
    //private Map<String, Operator> operatorMap;
    Map<String, Operator> operatorMap = new HashMap<String, Operator>();

    public PostFixCalculator () {
        Map<String, Operator> operatorMap = new HashMap<String, Operator>();  
        operatorMap.put("+", new AddOp());
        operatorMap.put("add", new AddOp());
        operatorMap.put("-", new SubOp());
        operatorMap.put("sub", new SubOp());
        operatorMap.put("/", new DivOp());
        operatorMap.put("div", new DivOp());
        operatorMap.put("*", new MultOp());
        operatorMap.put("mult", new MultOp());
        operatorMap.put("=", new PrintOp());
        operatorMap.put("print", new PrintOp());     
    }      
    public class AddOp implements Operator {
        public AddOp () {
        }
        public int numArgs () {
            return 2;
        }
        public double eval (List<Double> args) {
            double a = args.get(0);
            double b = args.get(1);
            double sum = a + b;
            return sum;
        }
    }
    public class SubOp implements Operator {
        public SubOp () {
        }

        public int numArgs () {
            return 2;
        }
        public double eval (List<Double> args) {
            double a = args.get(0);
            double b = args.get(1);
            double difference = a - b;
            return difference;
        }
    }
    public class DivOp implements Operator {
        public DivOp () {
        }

        public int numArgs () {
            return 2;
        }
        public double eval (List<Double> args) {
            double a = args.get(0);
            double b = args.get(1);
            double quotient = a / b;
            return quotient;
        }
    }
    public class MultOp implements Operator {
        public MultOp () {
        }

        public int numArgs () {
            return 2;
        }
        public double eval (List<Double> args) {
            double a = args.get(0);
            double b = args.get(1);
            double product = a * b;
            return product;
        }
    }
    public class PrintOp implements Operator {
        public PrintOp () {
        }
        public int numArgs () {
            return 1;
        }
        public double eval (List<Double> args) {
            System.out.println(myStack.pop());
            return 1;
        }
    }
    public void storeOperand (double operand) {
        myStack.push(operand);
    }
    public void evalOperator (String operator) {
        Operator o = operatorMap.get(operator);
        ArrayList<Double> evalList = new ArrayList<Double>();
        if (o.numArgs() == 2) {
            double a = myStack.pop();
            double b = myStack.pop();
            evalList.add(a);
            evalList.add(b);
        }
        else {
            double a = myStack.pop();
            evalList.add(a);
        }
        double answer = o.eval(evalList);
        myStack.push(answer);
    }
}

2 个答案:

答案 0 :(得分:2)

确保您正在导入正在使用的所有内容。在文件顶部,添加:

import java.util.ArrayList;

如果你正在使用Eclipse,你可以按Ctrl-Shift-O来修复你的导入。

答案 1 :(得分:0)

您仍然缺少以下导入:

import java.util.HashMap;
import java.util.List;
import java.util.Map;

然后检查与自定义类型相关的剩余错误。