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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.