Google Guava is a java library with lot of utilities and reusable components.
This requires the library guava-10.0.jar to be in classpath.
The following example shows using Preconditions.checkNotNull() API.
It throws NullPointerException if the given reference is null.
//NullPointerException - if reference is null
//Preconditions.checkNotNull(reference, errorMessage)
/**
* Negative case
*/ try {
Preconditions.checkNotNull(System.getProperty("MY_APP_HOME"),
"This program requires MY_APP_HOME system property");
} catch (NullPointerException e) {
System.out.println(e);
}
/**
* Positive case
*/
System.setProperty("MY_APP_HOME", "C:\\BTC");
Preconditions.checkNotNull(System.getProperty("MY_APP_HOME"),
"This program requires MY_APP_HOME system property");
System.out.println("Thanks for providing valid input");
}
}
It gives the following output,
java.lang.NullPointerException: This program requires MY_APP_HOME system property
Thanks for providing valid input