Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to require that an argument provided to a method is not null?

Is there a better way to require that an argument is not null in a method? I keep checking if any of the arguments that my method requires are null, as show below. But I'm wondering if there is a better way.

public void MyMethod(string a, int b)
{
   if(a==null){throw new ArgumentNullException("a");}
   if(b==null){throw new ArgumentNullException("b");}

   //more stuff here
}
like image 641
Eric Anastas Avatar asked Apr 16 '09 00:04

Eric Anastas


People also ask

What does the @NotNull annotation do?

The @NotNull annotation is, actually, an explicit contract declaring that: A method should not return null. Variables (fields, local variables, and parameters) cannot hold a null value.

IS NOT null Java method?

The nonNull method is a static method of the Objects class in Java that checks whether the input object reference supplied to it is non-null or not. If the passed object is non-null, then the method returns true. If the passed object is null , then the method returns false.

What is not null annotation in Spring boot?

Annotation Type NonNullA common Spring annotation to declare that annotated elements cannot be null . Leverages JSR-305 meta-annotations to indicate nullability in Java to common tools with JSR-305 support and used by Kotlin to infer nullability of Spring API.

What exception does @NotNull throw?

NullPointerException will be thrown with 'field name is marked non-null but is null' as the exception message.


2 Answers

Rick Brewster (author of Paint.NET) blogged about a Fluent API alternative:

http://blog.getpaint.net/2008/12/06/a-fluent-approach-to-c-parameter-validation/

like image 164
Martin Peck Avatar answered Oct 05 '22 11:10

Martin Peck


You can write some utility methods. This is the common pattern in java.

user code:

public void MyMethod(string a, int b)
{
    //validate each
    Objects.RequireNotNull(a);
    Objects.RequireNotNull(b);

    //or validate in single line as array 
    Objects.RequireNotNullArray(a, b);
}

implementation code:

public static class Objects
{
    public static T RequireNotNull<T>(T arg)
    {
        if(arg == null)
        {
            throw new ArgumentNullException();
        }
        return arg;
    }

    public static object[] RequireNotNullArray(params object[] args)
    {
        return RequireNotNullArray<object>(args);
    }

    public static T[] RequireNotNullArray<T>(params T[] args)
    {
        Objects.RequireNotNull(args);
        for(int i=0; i<args.Length; i++)
        {
            T arg = args[i];
            if(arg == null)
            {
                throw new ArgumentNullException($"null entry at position:{i}");
            }
        }
        return args;
    }

}

You cannot get the variable name in the exception. but with the stack trace and your source code, it should be possible to easily track down.

like image 31
Mohan Kumar Avatar answered Oct 05 '22 11:10

Mohan Kumar