vjk
vjk

Reputation: 2283

@NonNull not throwing compile error

import org.eclipse.jdt.annotation.NonNull;

  public class NullAnnotationTest {

        public void meth1(@NonNull String in) {
            System.out.println(in.length());
        }

        public static void main(String[] args) {
            NullAnnotationTest test = new NullAnnotationTest();

            test.meth1(null);
        }
    }

If we have @NonNull annotation, i thought we will get compile time error if we call meth1 with a null argument. But i am not seeing any sort of errors or warning in the above program. Can some correct me?

Upvotes: 0

Views: 1233

Answers (1)

sauumum
sauumum

Reputation: 1788

By default this feature i.e. Enable annotation based null analysis is DISABLE in eclipse.

Please enable this. Go to Window=>Preferences => Java=> Compiler => Error/Warning. Enable the setting as shown in enter image description here

Once you have this setting enable, you will get Null type mismatch: required '@NonNull String' but the provided value is null compile time error.

Upvotes: 2

Related Questions