0

I'm trying to get two nulls to compare, keeps throwing java.lang.NullPointerException in Eclipse

public Double x(String x){

        if (x.split(" ")[1].equals("+")) {
            // Ignore this stuff, it works
            this.x = Double.valueOf(x.split(" ")[0]);
            return new Double(x(Double.valueOf(x.split(" ")[2])));
        } else if (x.split(" ")[1].equals("x")) {
            // Ignore this stuff, it works
            this.x = Double.valueOf(x.split(" ")[0]);
            return new Double(x(Double.parseDouble(x.split(" ")[2])));
        } else {
            // The problem
            return new Double(null);
        }
    }

public boolean testParser() {

    boolean parseOne;
    boolean parseTwo;
    boolean parseThree;

        if (calc.x("12 + 5") == 17) {
            System.out.println("[ OK ] Parser adds correctly.");
            parseOne = true;
        } else {
            System.out.println("[FAIL] Basic parsing fails to add.");
            parseOne = false;
        }
        if (calc.x("12 x 5") == 60) {
            System.out.println("[ OK ] Parser multiplies correctly.");
            parseTwo = true;
        } else {
            System.out.println("[FAIL] Basic parsing fails to multiply.");
            parseTwo = false;
        }

        // Comparing null with null here not working
        if (calc.x("12 [ 3") == null) {
            System.out.println("[ OK ] Parser returns null for operators which are not supported.");
            parseThree = true;
        } else {
            System.out.println("[FAIL] Parser does not return null for operators which are not supported.");
            parseThree = false;
        }

        return (parseOne && parseTwo && parseThree);    
    }

(Where calc is just the name of an instance of an object which the Double x(String x) method is in) Can anyone suggest a way to resolve this?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Thahleel al-Aleem
  • 711
  • 2
  • 10
  • 20

2 Answers2

3

Here's what new Double(null) does...

The compiler has to find a constructor that matches. There are two constructors for the Double class (according to the javadoc):

Double(double value)
    // Constructs a newly allocated Double object that represents the primitive double argument.
Double(String s)
    // Constructs a newly allocated Double object that represents the floating-point value of type double represented by the string.

null is not a valid value for a primitive type (i.e. small-d double), so this results in an attempt to call the second constructor, that takes a String. At runtime, it then tries to parse the string in the format of a double. And this results in a NullPointerException because the String parameter is null. [To be more precise, I think at some point there will be a call to s.trim() from the class sun.misc.FloatingDecimal, which throws an exception if s is null.]

ajb
  • 31,309
  • 3
  • 58
  • 84
1

You are getting the null pointer exception because you are calling

new Double(null);

This is not how you use the Double constructor. To pass back null, simply return null. You can fix this by changing this line to:

return null;
dhalik
  • 455
  • 3
  • 11