Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of Android Studio error: Not annotated parameter overrides @NonNull parameter

I'm trying out Android Studio. Upon creating a new project and adding a default onSaveInstanceState method to the create MyActivity class, when I try to commit the code to Git, I get a strange error I don't understand. The code is this:

The error I get is this:

enter image description here

If I try to change the method signature to protected void onSaveInstanceState(@NotNull Bundle outState), then the IDE tells me it can't resolve the symbol NotNull.

What do I need to do to get rid of the warning?

like image 818
Monomo Avatar asked Jul 14 '14 01:07

Monomo


3 Answers

It's an annotation, but the correct name is NonNull:

protected void onSaveInstanceState(@NonNull Bundle outState)

(And also)

import android.support.annotation.NonNull;

The purpose is to allow the compiler to warn when certain assumptions are being violated (such as a parameter of a method that should always have a value, as in this particular case, although there are others). From the Support Annotations documentation:

The @NonNull annotation can be used to indicate that a given parameter can not be null.

If a local variable is known to be null (for example because some earlier code checked whether it was null), and you pass that as a parameter to a method where that parameter is marked as @NonNull, the IDE will warn you that you have a potential crash.

They are tools for static analysis. Runtime behavior is not altered at all.


In this case, the particular warning is that the original method you're overriding (in Activity) has a @NonNull annotation on the outState parameter, but you did not include it in the overriding method. Just adding it should fix the issue, i.e.

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
}
like image 196
matiash Avatar answered Oct 25 '22 03:10

matiash


A number of useful support annotations were recently added in the Android support library. Their primary role is to annotate properties of various methods and parameters to help catch bugs. For example, if you pass null value to a parameter that is marked with the NotNull annotation you will get a warning.

The annotations can be added to your project with Gradle by adding the following dependency:

dependencies {
    compile 'com.android.support:support-annotations:20.0.0'
}

You are getting the warning because the Bundle parameter is marked with the @NotNull annotation and by overriding the method the annotation gets hidden. The right thing to do is to add the annotation to the overriden method's parameter as well.

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
}
like image 15
LukaCiko Avatar answered Oct 25 '22 04:10

LukaCiko


In addition to the other answers, the @NonNull (and it's opponent, @Nullable) annotation annotates a field, parameter or method return type. IntelliJ and thus Android Studio can warn you for possible NullPointerExceptions at compile time.

An example is best here:

@NonNull private String myString = "Hello";

@Nullable private String myOtherString = null;

@NonNull 
public Object doStuff() {
    System.out.println(myString.length); // No warning
    System.out.println(doSomething(myString).length); // Warning, the result might be null.

    doSomething(myOtherString); // Warning, myOtherString might be null.

    return myOtherString; // Warning, myOtherString might be null.
}

@Nullable
private String doSomething(@NonNull String a) {
    return a.length > 1 ? null : a; // No warning
}

These annotations do not alter runtime behavior (although I have experimented with this), but serve as a tool for preventing bugs.

Note that the message you received was not an error, but just a warning, which is safe to ignore, if you choose to. The alternative is to annotate the parameter yourself as well, as Android Studio suggests:

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
}
like image 9
nhaarman Avatar answered Oct 25 '22 04:10

nhaarman