Search code examples
androidnullpointerexceptionannotationscontract

What is main use of "Add method contract to ..." Android Studio


I am getting following option like "Add method contract to 'setDisplayHomeAsUpEnabled'", I know that inspection is because of NPE can throw by getSupportActionBar() but I want to know actual use of Method Contract

enter image description here

If I Press Enter on that selection, I am getting:

enter image description here

Any Sharing? Any Help? It will appreciated :)


Solution

  • This relates to creating method contracts that describe when a method may return null or not null.

    IntelliJ/Android Studio is sometimes able to to infer this from the context of the code. But when it is not able to know for sure, you can specify a method contract that tells the caller what will happen in different circumstances.

    Read more about method contracts here.