Results 1 to 3 of 3
- 01-12-2010, 12:01 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 13
- Rep Power
- 0
Why do I get a NullPointerException?
I'm having a go at writing a calculator. The Calculator is the controlling class, which calls all the other ones. The Evaluator is the one that does the calculations.
Java Code:package calculator; public class Calculator { /* * The main class that controls the program. */ public String currentCalculation; Reader r; Evaluator e; Splitter s; public Calculator() { r = new Reader(); s = new Splitter(); e = new Evaluator(); } private void calculate() { /* * Keeps the calculator calculating: * - reads in the current calculation * - splits it into a string[] to be analysed * - analyses the string[] for brackets * - calculates and prints out the answer * - starts again */ currentCalculation = r.prompt(); System.out.println("Read in."); String[] currentArray = s.split(currentCalculation); System.out.println("Array split."); e.evaluate(currentArray); System.out.println("E evaluated."); calculate(); } public static void main(String[] args) { System.out.println("Type quit at any point to exit"); Calculator c = new Calculator(); c.calculate(); } }My reader class works just fine, but everytime I try to run the program and input a calculation I receive the following:Java Code:package calculator; public class Evaluator { private String add = "+"; private String subtract = "-"; private String multiply = "*"; private String divide = "/"; public Evaluator() { } public String evaluate(String[] s) { String[] newS = null; String result = null; System.out.println(s.length); if (s.length != 1) { for (int i = 0; i < s.length; i++) { if (s[i].equals(divide)) { System.out.println("divide"); s[i] = divide(s[i-1], s[i+1]); System.out.println("newS"); newS = new String[s.length-2]; for (int j = 0, k = 0; j < s.length; j++) { if (s[j].equals(s[i-1]) || s[j].equals(s[i+1])) { // ignore them } else { newS[k++] = s[j]; } } } if (newS != null) { evaluate(newS); } } } else if (s.length == 1) { result = s[0].toString(); } else { System.out.println("No result found."); result = "0"; } return result; } private String divide(String s1, String s2) { Double d1 = Double.parseDouble(s1); Double d2 = Double.parseDouble(s2); Double result = d1 / d2; return result.toString(); } }
java calculator/Calculator
Type quit at any point to exit
>>> 8 / 5
8 / 5
Read in.
8
/
5
Array split.
Exception in thread "main" java.lang.NullPointerException
at calculator.Evaluator.evaluate(Evaluator.java:17)
at calculator.Calculator.calculate(Calculator.java:35 )
at calculator.Calculator.main(Calculator.java:45)
Have I missed initialising something? Where am I pointing to nothing? :confused:
Any help would be much appreciated!
- 01-12-2010, 12:29 PM #2
Member
- Join Date
- Jan 2010
- Posts
- 13
- Rep Power
- 0
Don't worry! Found my problem!
- 01-12-2010, 04:02 PM #3
Similar Threads
-
NullPointerException: I can't get rid of it.
By mcashe in forum AWT / SwingReplies: 2Last Post: 08-17-2009, 09:16 PM -
NullPointerException
By mjz in forum JDBCReplies: 1Last Post: 08-06-2009, 11:46 AM -
NullPointerException
By adeeb in forum AWT / SwingReplies: 3Last Post: 06-11-2008, 08:42 AM -
NullPointerException
By mensa in forum Java 2DReplies: 5Last Post: 05-03-2008, 11:19 PM -
NullPointerException
By Feng in forum New To JavaReplies: 5Last Post: 11-24-2007, 07:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks