22

I am using Retrofit 2 for get response from my API and store its value in my constant like below

if(response.isSuccessful()) {
                    constant.banner_on = response.body().getBanner_on();
                    constant.int_on = response.body().getInt_on();
                    constant.int_click = response.body().getInt_click();
                }

It's giving me warning on all three like below

Method invocation getBanner_on may produce java.lang.nullPointerException

I am confused and unable to resolve this warning. Let me know if someone can help me to come out from this.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Priya
  • 1,602
  • 4
  • 22
  • 37
  • 1
    This is giving a warning because the response may be null, so for safe side Android Studio gives warning, for not getting the exception you can assert `response!=null` and the continue on. – Rakshit Nawani Oct 02 '17 at 04:16
  • Yes, this may be possible the response may be null so wrap the code into a if statement it is good to save your app by `NullPointerExecption` as in your previous question – UltimateDevil Oct 02 '17 at 04:22
  • 1
    Does this answer your question? [Why should one use Objects.requireNonNull()?](https://stackoverflow.com/questions/45632920/why-should-one-use-objects-requirenonnull) – Vega Sep 26 '20 at 04:56

4 Answers4

25

It is just a warning as it will never be null if the response is successful. You can ignore it or wrap around if(response.body() != null) to remove the warning.

Ads ads = response.body();
if(ads != null){
    constant.banner_on = ads.getBanner_on();
    // and so on.
}
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
5

Using if is great but there is only one line, much cleaner way is:

constant.banner_on = ads != null ? ads.getBanner_on() : null;

If you are using Java 8, you can do a assertion before assignment:

Ads ads = response.body();
assert ads != null;
constant.banner_on = ads.getBanner_on();

Another way to do this is using Objects.requireNonNull() before assignment:

constant.banner_on = Objects.requireNonNull(ads.getBanner_on());

That is actually designed primarily for param validation. Source code comment:

/**
     * Checks that the specified object reference is not {@code null}. This
     * method is designed primarily for doing parameter validation in methods
     * and constructors, as demonstrated below:
     * <blockquote><pre>
     * public Foo(Bar bar) {
     *     this.bar = Objects.requireNonNull(bar);
     * }
     * </pre></blockquote>
     * 

One great SO answer about this is here. Also read this to get understanding why we need to explicitly check..

Blasanka
  • 21,001
  • 12
  • 102
  • 104
2

Just use this null pointer check.

If(response != null && response.isSuccessfull())
{

// body

}
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
emilpmp
  • 1,716
  • 17
  • 32
  • @priya i am not sure about this, but can you check null pointer for response.getBody() as well? That is if (response !=null && respone.getBody() ! =Null) { ... } – emilpmp Oct 02 '17 at 04:35
0

Its a good habit to before assigning any value from response handle null pointer exception to prevent some of exception. And you can also use try and catch to handle other exceptions.

 if(response.isSuccessful()) {
    try {
         if(response.body() != null){
                constant.banner_on = response.body().getBanner_on();
                constant.int_on = response.body().getInt_on();
                constant.int_click = response.body().getInt_click();
           }
     } catch (IOException e) {
        e.printStackTrace();
     }
   }
Upendra Shah
  • 2,218
  • 17
  • 27