Results 1 to 3 of 3
  1. #1
    nessa203 is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Question 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();
    	}
    }
    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();
    	}
    
    }
    My reader class works just fine, but everytime I try to run the program and input a calculation I receive the following:

    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!

  2. #2
    nessa203 is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    Don't worry! Found my problem!

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

Similar Threads

  1. NullPointerException: I can't get rid of it.
    By mcashe in forum AWT / Swing
    Replies: 2
    Last Post: 08-17-2009, 09:16 PM
  2. NullPointerException
    By mjz in forum JDBC
    Replies: 1
    Last Post: 08-06-2009, 11:46 AM
  3. NullPointerException
    By adeeb in forum AWT / Swing
    Replies: 3
    Last Post: 06-11-2008, 08:42 AM
  4. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-03-2008, 11:19 PM
  5. NullPointerException
    By Feng in forum New To Java
    Replies: 5
    Last Post: 11-24-2007, 07:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •