Assert/Checks if the given object is null. - Java java.lang

Java examples for java.lang:Assert

Description

Assert/Checks if the given object is null.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object anObject = "java2s.com";
        String aName = "java2s.com";
        isNotNull(anObject, aName);/*from  www  .j  ava2 s .  c  o m*/
    }

    /**
     * Checks if the given object is null.
     * If the object is null, an IllegalArgumentException is thrown.
     * 
     * @param anObject
     *            The object to validate.
     * @param aName
     *            The name of the parameter.
     */
    public static void isNotNull(final Object anObject, final String aName) {
        if (anObject == null) {
            throw new IllegalArgumentException("The argument '" + aName
                    + "' must not be null!");
        }
    }
}

Related Tutorials