tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Google Guava > Basic > Check Not Null Precondition

Check Not Null Precondition 

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.

File Name  :  
com/bethecoder/tutorials/guava/base_tests/CheckNotNullPreTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.guava.base_tests;

import com.google.common.base.Preconditions;

public class CheckNotNullPreTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    //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



 
  


  
bl  br