Avoid NullPointerException on Primitive Wrapper Objects

From JavaWiki.org
Jump to: navigation, search

Using the primitive wrapper objects like Boolean, Integer or Long in operators may throw NullPointerException, if the value is null, because the primitive wrapper objects auto unboxed to primitive values when you use java operators like '==' '>' '<', etc.

e.g.

private Boolean isDone() {
    return null;
}

private Long getMyLongValue() {
    return null;
}

public void someMethod() {
    // Throw NPE
    if (isDone()) {
    }

    // Throw NPE
    if (getMyLongValue() > 10) {
    }
}

This will throw NullPointerException if the method 'isDone()' or 'getMyLongValue()' returns null.

To avoid NPE it needs to be null checked as below


if (isDone() != null && isDone())

if (getMyLongValue() != null && getMyLongValue() > 10)


To avoid this kind null check every if condition we can create utility class which returns 0 if it is null on numbers and false on Boolean.

e.g.

public class Primitive {
    public static int value(final Integer val) {
        return value(val, 0);
    }

    public static int value(final Integer val, final int defaultVal) {
        return val == null ? defaultVal : val.intValue();
    }
}

using this class you can safely compare with primitive values

if (Primitive.value(getMyLongValue()) > 10)


Following class provides the full implementation:

package poc.lang;

public class Primitive {
    /* Byte */
    public static byte value(final Byte wrapper) {
        return value(wrapper, (byte) 0);
    }

    public static byte value(final Byte wrapper, final byte ifNullValue) {
        return wrapper == null ? ifNullValue : wrapper.byteValue();
    }

    /* Short */
    public static short value(final Short wrapper) {
        return value(wrapper, (short) 0);
    }

    public static short value(final Short wrapper, final short ifNullValue) {
        return wrapper == null ? ifNullValue : wrapper.shortValue();
    }

    /* Integer */
    public static int value(final Integer wrapper) {
        return value(wrapper, 0);
    }

    public static int value(final Integer wrapper, final int ifNullValue) {
        return wrapper == null ? ifNullValue : wrapper.intValue();
    }

    /* Long */
    public static long value(final Long wrapper) {
        return value(wrapper, 0L);
    }

    public static long value(final Long wrapper, final long ifNullValue) {
        return wrapper == null ? ifNullValue : wrapper.longValue();
    }

    /* Float */
    public static float value(final Float wrapper) {
        return value(wrapper, 0F);
    }

    public static float value(final Float wrapper, final float ifNullValue) {
        return wrapper == null ? ifNullValue : wrapper.floatValue();
    }

    /* Double */
    public static double value(final Double wrapper) {
        return value(wrapper, 0D);
    }

    public static double value(final Double wrapper, final double ifNullValue) {
        return wrapper == null ? ifNullValue : wrapper.doubleValue();
    }

    /* Boolean */
    public static boolean value(final Boolean wrapper) {
        return value(wrapper, false);
    }

    public static boolean value(final Boolean wrapper, final boolean ifNullValue) {
        return wrapper == null ? ifNullValue : wrapper.booleanValue();
    }
}

Utility class for comparing primitive values

package poc.lang;

public class Check {
    public static boolean isTrue(final Boolean wrapper) {
        return Primitive.value(wrapper);
    }

    public static boolean isFalse(final Boolean wrapper) {
        return !Primitive.value(wrapper);
    }

    /* Byte */
    public static boolean between(final Byte wrapper, final byte from, final byte to) {
        byte value = Primitive.value(wrapper);
        return value >= from && value <= to;
    }

    public static boolean in(final Byte wrapper, final byte... values) {
        if (wrapper == null || values == null) {
            return false;
        }

        byte wrapperVal = Primitive.value(wrapper);

        // Java 8
        // return Arrays.stream(values).anyMatch(val -> wrapperVal == val);

        for (byte val : values) {
            if (wrapperVal == val) {
                return true;
            }
        }

        return false;
    }

    /* Short */
    public static boolean between(final Short wrapper, final short from, final short to) {
        short value = Primitive.value(wrapper);
        return value >= from && value <= to;
    }

    public static boolean in(final Short wrapper, final short... values) {
        if (wrapper == null || values == null) {
            return false;
        }

        short wrapperVal = Primitive.value(wrapper);
        for (short val : values) {
            if (wrapperVal == val) {
                return true;
            }
        }

        return false;
    }

    /* Integer */
    public static boolean between(final Integer wrapper, final int from, final int to) {
        int value = Primitive.value(wrapper);
        return value >= from && value <= to;
    }

    public static boolean in(final Integer wrapper, final int... values) {
        if (wrapper == null || values == null) {
            return false;
        }

        int wrapperVal = Primitive.value(wrapper);
        for (int val : values) {
            if (wrapperVal == val) {
                return true;
            }
        }

        return false;
    }

    /* Long */
    public static boolean between(final Long wrapper, final long from, final long to) {
        long value = Primitive.value(wrapper);
        return value >= from && value <= to;
    }

    public static boolean in(final Long wrapper, final long... values) {
        if (wrapper == null || values == null) {
            return false;
        }

        long wrapperVal = Primitive.value(wrapper);
        for (long val : values) {
            if (wrapperVal == val) {
                return true;
            }
        }

        return false;
    }

    /* Float */
    public static boolean between(final Float wrapper, final float from, final float to) {
        float value = Primitive.value(wrapper);
        return value >= from && value <= to;
    }

    public static boolean in(final Float wrapper, final float... values) {
        if (wrapper == null || values == null) {
            return false;
        }

        float wrapperVal = Primitive.value(wrapper);
        for (float val : values) {
            if (wrapperVal == val) {
                return true;
            }
        }

        return false;
    }

    /* Double */
    public static boolean between(final Double wrapper, final double from, final double to) {
        double value = Primitive.value(wrapper);
        return value >= from && value <= to;
    }

    public static boolean in(final Double wrapper, final double... values) {
        if (wrapper == null || values == null) {
            return false;
        }

        double wrapperVal = Primitive.value(wrapper);
        for (double val : values) {
            if (wrapperVal == val) {
                return true;
            }
        }

        return false;
    }
}