Sdílet prostřednictvím


Objects.RequireNonNull Method

Definition

Overloads

RequireNonNull(Object)

Checks that the specified object reference is not null.

RequireNonNull(Object, ISupplier)

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

RequireNonNull(Object, String)

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

RequireNonNull(Object)

Checks that the specified object reference is not null.

[Android.Runtime.Register("requireNonNull", "(Ljava/lang/Object;)Ljava/lang/Object;", "")]
[Java.Interop.JavaTypeParameters(new System.String[] { "T" })]
public static Java.Lang.Object RequireNonNull (Java.Lang.Object? obj);
[<Android.Runtime.Register("requireNonNull", "(Ljava/lang/Object;)Ljava/lang/Object;", "")>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "T" })>]
static member RequireNonNull : Java.Lang.Object -> Java.Lang.Object

Parameters

obj
Object

the object reference to check for nullity

Returns

obj if not null

Attributes

Remarks

Checks that the specified object reference is not null. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below: <blockquote>

public Foo(Bar bar) {
                this.bar = Objects.requireNonNull(bar);
            }

</blockquote>

Java documentation for java.util.Objects.requireNonNull(T).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

RequireNonNull(Object, ISupplier)

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

[Android.Runtime.Register("requireNonNull", "(Ljava/lang/Object;Ljava/util/function/Supplier;)Ljava/lang/Object;", "", ApiSince=24)]
[Java.Interop.JavaTypeParameters(new System.String[] { "T" })]
public static Java.Lang.Object RequireNonNull (Java.Lang.Object? obj, Java.Util.Functions.ISupplier messageSupplier);
[<Android.Runtime.Register("requireNonNull", "(Ljava/lang/Object;Ljava/util/function/Supplier;)Ljava/lang/Object;", "", ApiSince=24)>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "T" })>]
static member RequireNonNull : Java.Lang.Object * Java.Util.Functions.ISupplier -> Java.Lang.Object

Parameters

obj
Object

the object reference to check for nullity

messageSupplier
ISupplier

supplier of the detail message to be used in the event that a NullPointerException is thrown

Returns

obj if not null

Attributes

Remarks

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

Unlike the method #requireNonNull(Object, String), this method allows creation of the message to be deferred until after the null check is made. While this may confer a performance advantage in the non-null case, when deciding to call this method care should be taken that the costs of creating the message supplier are less than the cost of just creating the string message directly.

Added in 1.8.

Java documentation for java.util.Objects.requireNonNull(T, java.util.function.Supplier<java.lang.String>).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

RequireNonNull(Object, String)

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

[Android.Runtime.Register("requireNonNull", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;", "")]
[Java.Interop.JavaTypeParameters(new System.String[] { "T" })]
public static Java.Lang.Object RequireNonNull (Java.Lang.Object? obj, string message);
[<Android.Runtime.Register("requireNonNull", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;", "")>]
[<Java.Interop.JavaTypeParameters(new System.String[] { "T" })>]
static member RequireNonNull : Java.Lang.Object * string -> Java.Lang.Object

Parameters

obj
Object

the object reference to check for nullity

message
String

detail message to be used in the event that a NullPointerException is thrown

Returns

obj if not null

Attributes

Remarks

Checks that the specified object reference is not null and throws a customized NullPointerException if it is. This method is designed primarily for doing parameter validation in methods and constructors with multiple parameters, as demonstrated below: <blockquote>

public Foo(Bar bar, Baz baz) {
                this.bar = Objects.requireNonNull(bar, "bar must not be null");
                this.baz = Objects.requireNonNull(baz, "baz must not be null");
            }

</blockquote>

Java documentation for java.util.Objects.requireNonNull(T, java.lang.String).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to