-
Notifications
You must be signed in to change notification settings - Fork 1
NullPointerExceptions
mrumpf edited this page Jun 25, 2012
·
3 revisions
Some of the utility classes focus on making the live easier to avoid NullPointerExceptions.
The 2 Utility Classes ObjectUtil
and StringUtil
in the org.jcoderz.commons.util
package hold some methods to deal with null values without the need to write complex nested if statements:
-
public static boolean org.jcoderz.commons.util.ObjectUtil.equals(Object a, Object b)
Used to compare two objects that might be null without risking a NullPointerException. -
public static String org.jcoderz.commons.util.ObjectUtil.toStringOrEmpty(Object obj)
Used to convert a Object to its String representation, or to an empty String, if the Object is null. -
public static boolean org.jcoderz.commons.util.StringUtil.isEmptyOrNull(String s)
Test if a String is either empty or null. Can be used when both the empty string as well as the null value are treated in the same way. -
public static boolean org.jcoderz.commons.util.StringUtil.isBlankOrNull(String s)
Test if a String contains no or only whitespace characters or is null. This is commonly used in UIs where an entered whitespace should be treated like no input at all.
There are also powerful bug detectors available with FindBugs eg. NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE
or NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE
.