Java Assert Not Null assertNotNull(T object, String fieldName)

Here you can find the source of assertNotNull(T object, String fieldName)

Description

Asserts that the given object is non-null and returns it.

License

Amazon Software License

Parameter

Parameter Description
object Object to assert on
fieldName Field name to display in exception message if null
T the type of object.

Return

Object if non null

Declaration

public static <T> T assertNotNull(T object, String fieldName) 

Method Source Code

//package com.java2s;
//License from project: Amazon Software License 

public class Main {
    /**/*from w w  w.j a v  a  2 s  .co m*/
     * Asserts that the given object is non-null and returns it.
     *
     * @param object Object to assert on
     * @param fieldName Field name to display in exception message if null
     * @param <T> the type of object.
     * @return Object if non null
     */
    public static <T> T assertNotNull(T object, String fieldName) {
        if (object == null) {
            throw new IllegalArgumentException(String.format("%s cannot be null", fieldName));
        }
        return object;
    }
}

Related

  1. assertNotNull(String variableName, Object value)
  2. assertNotNull(StringBuilder buffer)
  3. assertNotNull(T exception, Object... objects)
  4. assertNotNull(T o)
  5. assertNotNull(T obj)
  6. assertNotNull(T value, String message)
  7. assertNotNullArg(Object value, String argName)
  8. assertNotNullArgument(Object obj, String argName)
  9. assertNotNullEightBytes(byte[] bytes)