Last Updated: February 25, 2016
·
812
· raulraja

Check for 'null' values

This static utility allows you to check for any arbitrary number of values, ensuring none of them are null.

/**
 * Validates that all values are set.
 *
 * @param values a varargs array of arguments
 */
public static void defenseNotNull(Object... values) {
    if (values == null) {
        throw new IllegalArgumentException("values is null");
    }
    for (int i = 0; i < values.length; i++) {
        Object value = values[i];
        if (value == null) {
            throw new IllegalArgumentException(String.format("values[%d] is null", i));
        }
    }
}

Use as:

defenseNotNull(a, b, c, ...);