Skip to content

Latest commit

 

History

History
49 lines (39 loc) · 892 Bytes

README.md

File metadata and controls

49 lines (39 loc) · 892 Bytes

SafeCallJava

A simple class to prevent NullPointerExceptions in Java

Java

import java.util.function.Supplier;

public class SafeCall {

    /**
     * Prevents NullPointerException
     */
    public static <T> T get(Supplier<T> supplier) {
        try {
            return supplier.get();
        } catch (NullPointerException e) {
            return null;
        }
    }
}

Kotlin

object SafeCall {

    /**
     * Prevents [NullPointerException] in java
     */
    @JvmStatic
    fun <T : Any> get(block: () -> T): T? {
        return try {
            block.invoke()
        } catch (e: NullPointerException) {
            null
        }
    }
}

Usage in Java

SafeCall.get(() -> getSomeObj().getProperty());

If getSomeObj method returns null, instead of occurence of a NullPointerException, SafeCall::get will return null.