简体   繁体   English

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

[英]NullPointerException when trying to run PostFixCalculator in Java

I have gotten my code to work to the extent that it can compile and run but now I get exceptions at two different places as follows: 我已经让我的代码工作到可以编译和运行的程度,但现在我在两个不同的地方得到例外如下:

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

...I am not sure at all what is wrong. ......我根本不确定是什么问题。 The first error is in the code that follows at 第一个错误出现在随后的代码中

myStack.push(operand);

But I am so lost as to where to go from here... 但我迷失在哪里从这里出发......

    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);
    }
}

Make sure you're importing everything you are using. 确保您导入正在使用的所有内容。 At the top of your file, add: 在文件的顶部,添加:

import java.util.ArrayList;

If you're using Eclipse you can press Ctrl-Shift-O to fix your imports. 如果您正在使用Eclipse,则可以按Ctrl-Shift-O来修复导入。

You are still missing the following imports: 您仍然缺少以下导入:

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

Then check the remaining errors related to your custom types. 然后检查与自定义类型相关的其余错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在NetBeans上运行应用程序时出现java.lang.NullPointerException错误 - java.lang.NullPointerException bug when trying to run an app on netbeans java NullPointerException尝试绘制时 - java NullPointerException when trying to draw 尝试使用 Java 在 Selenium 中运行测试用例时,线程“主”java.lang.NullPointerException 出现异常 - Exception in thread "main" java.lang.NullPointerException when trying to run a test case in Selenium with Java 尝试运行.jar文件时出现NullPointerException - NullPointerException when trying to run .jar file 尝试使用cassandra数据存储区从eclipse运行nutch时获取java.lang.NullPointerException - getting java.lang.NullPointerException when trying to run nutch from eclipse with a cassandra datastore 尝试运行简单的“Hello!”示例脚本时,SBT会给出java.lang.NullPointerException - SBT gives java.lang.NullPointerException when trying to run simple “Hello!” example script 尝试在Apache Tomcat上从Eclipse运行Servlet项目时的java.lang.NullPointerException - java.lang.NullPointerException when trying to run Servlet project from Eclipse on Apache Tomcat NullPointerException尝试调用Abstract类时-Java - NullPointerException When trying to call Abstract class - Java 尝试初始化 Java 接口时出现 NullPointerException - NullPointerException when trying to initialize Java interfaces Android开发:尝试获取getStringExtra()时出现java NullPointerException - Android Development: java NullPointerException When Trying To getStringExtra()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM