You're never initializing calcOperands
- so on line 60, when you use:
calcOperands.add(operand);
... calcOperands
is still null, causing the exception. It looks like you have the same problem for your other ArrayList
variables. You can just create a new list as part of the declaration - and I'd also change the variables themselves to just List<E>
, like this:
private final List<Double> calcOperands = new ArrayList<Double>();
private final List<String> calcOperators = new ArrayList<String>();
private final String stringInput;
private final List<Integer> priorityList = new ArrayList<String>();
private String[] splitString;
I've made most of the variables final too, as a matter of course. You might want to consider whether you really want splitString
to be mutable, only set if you call checkInput
...
(I haven't looked at the rest of your code.)
Besides just fixing these problems, you should take a step back and think about the diagnostics side of things. Which bit were you stuck on when trying to find the problem? Had you looked at the stack trace and pinpointed the problem to StringCalculator.java
line 60? Do you understand what NullPointerException
means? (And if not, had you tried to find out?) Now that you know the actual answer, it's worth thinking about how you could have reached it yourself - diagnosing issues is an important part of software development.