Skip to content

Instantly share code, notes, and snippets.

@bmchild
Created July 18, 2012 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmchild/3137404 to your computer and use it in GitHub Desktop.
Save bmchild/3137404 to your computer and use it in GitHub Desktop.
Checks if any number of objects are null
/**
* checks through a bunch of objects for a null value
*
* @param o1
* @param objects
* @return true if any object is null, false otherwise
*/
public static boolean hasNullValue(Object o1, Object... objects) {
if(o1 == null) {
return true;
}
boolean isNull = false;
for(Object o : objects) {
if(o == null) {
isNull = true;
break;
}
}
return isNull;
}
@Test
public void testHasNullValue() {
Object one = null;
assertTrue(Util.hasNullValue(one));
one = new Object();
assertFalse(Util.hasNullValue(one));
Object two = null;
assertTrue(Util.hasNullValue(one, two));
two = new Object();
assertFalse(Util.hasNullValue(one, two));
Object three = null;
assertTrue(Util.hasNullValue(one, two, three));
three = new Object();
assertFalse(Util.hasNullValue(one, two, three));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment