In my code there is a method to get some data from the API.
methods can return null. Is using @NonNull Call<InitGet> call
is good solution in this case? Or should I do a simple null check using if-else to not get a null pointer exception ?
@NonNull
Doesn't change your code, it's a marker to say to the client of the method "this code is not expected to return null"
Therefore if you can still return null you should not use that annotation.
But you are on the right track - not wanting to return null, so that is a good thing.
Like you said, You can change your method to something like this which is "null safe":
@NonNull
public String getFoo() {
String foo = something.getFoo();
if(foo == null) {
return "Foo was null, but this is a nice msg for the UI."; // or similar
} else {
return foo;
}
}
In your case "aes key" sounds like something that you either have it or you don't - and if you don't then it's pretty bad. I would do this:
@NonNull
public String getAesKey() {
String key = response.body().getAsKy();
if(key == null) {
throw new IllegalStateException(new NullPointerException("Key was null, but we need a key or everything is broken! Fail fast."));
} else {
return key;
}
}