0

I'm attempting to parse a String into a double with Double.parseDouble();, however I'm getting the following error:

Exception in thread "main"  35.5
java.lang.NullPointerException
    at Caluclator/userInput.Calculation.addMinus(Calculation.java:57)
    at Caluclator/userInput.Calculation.parse(Calculation.java:28)
    at Caluclator/userInput.Input.main(Input.java:12)

I cant find where I'm going wrong.

package userInput;

public class Calculation {
    public Double answer;

    public  Calculation() {
        }

    //Splitting string for 
    public static Double parse(String input) {
        String[] operator = new String[20];
        String[] delimiter = input.split("\\+|-");
        Integer counter = 0;

        //this searches to see if there is an operator
        for(Integer i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if(c == '+'||c == '-'){
                operator[counter] = Character.toString(c);
                counter++;
            }

        }
        delimiter = multDiv(delimiter);
        for(String element: delimiter) {
            System.out.println(element);
        }
        return addMinus(delimiter,operator,counter);//ERROR
    }


    //Multiplies the numbers
    public static String[] multDiv (String[] delimiter) {
        for(Integer y = 0; y < delimiter.length ; y++) {//iterates through elements in delimiter

            for(Integer i = 0; i < delimiter[y].length(); i++) {//iterates through chars in element string
                if(delimiter[y].charAt(i) == '*') {
                    String[] nums = delimiter[y].split("\\*");
                    delimiter[y]= String.valueOf(Double.parseDouble(nums[0])*Double.parseDouble(nums[1]));
                }
                else if(delimiter[y].charAt(i) == '/') {
                    String[] nums = delimiter[y].split("/");
                    delimiter[y] =String.valueOf(Double.parseDouble(nums[0])/Double.parseDouble(nums[1]));
                }

            }
        }
    return delimiter;
    }

/////*****SEARCHES FOR ADDS OR SUBTRACTS THE NUMBERS *****////
    public static Double addMinus(String[] numbers, String[] symbols, Integer counter) {
        double answ = Double.parseDouble(numbers[0]);
            Integer x = 1;

            for(Integer i = 0; i < symbols.length; i++) {
                if(symbols[i].contentEquals("-")) {//ERROR
                    double num = Double.parseDouble(numbers[x]);
                    answ = answ - num;
                    System.out.println(" "+answ);
                }
                else if(symbols[i].contentEquals("+")) {
                    double num = Double.parseDouble(numbers[x]);
                    answ = answ + num ;
                    System.out.println(" "+answ);
                }
                x++;
            }
            System.out.println(answ);
    return answ;
    }

Aziziz14
  • 35
  • 3

1 Answers1

0

Not all points in operator are filled. This is because you're assuming the input contains at least 20 + or - characters, but if it doesn't, then you're accessing a null String. There are two fixes I can think of:

Option 1:

        for(Integer i = 0; i < symbols.length; i++) {
            if(symbols[i] != null && symbols[i].contentEquals("-")) {//ERROR
                double num = Double.parseDouble(numbers[x]);
                answ = answ - num;
                System.out.println(" "+answ);
            }
            else if(symboles[i] != null && symbols[i].contentEquals("+")) {
                double num = Double.parseDouble(numbers[x]);
                answ = answ + num ;
                System.out.println(" "+answ);
            }
            x++;
        }

Every iteration, first check if it isn't null, then continue.

Option 2:

        for(Integer i = 0; i < symbols.length && symbols[i] != null; i++) {
            if(symbols[i].contentEquals("-")) {//ERROR
                double num = Double.parseDouble(numbers[x]);
                answ = answ - num;
                System.out.println(" "+answ);
            }
            else if(symbols[i].contentEquals("+")) {
                double num = Double.parseDouble(numbers[x]);
                answ = answ + num ;
                System.out.println(" "+answ);
            }
            x++;
        }

Since your filling operations from the start, once you reach a null String, stop, you're done.

Note: operator doesn't need to be a String[], it can just be a char[] because you're either putting a - or + sign, no String's larger than size 1.

  • Thank you. I originally thought i was parsing through cells that didn't exist. However this make much more sense. Thanks! – Aziziz14 Mar 21 '20 at 19:15