M. Haché M. Haché - 6 months ago 9
Java Question

Best way to prevent NullPointerException warning?

I had this warning in Android-studio that told me:


Method invocation 'data.getExtras().get("address").toString()' may produce 'java.lang.NullPointerException'


So I changed my code to get rid of that warning.

// Function to read the result from newly created activity
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100 && data.getExtras().get("x") != null &&
data.getExtras().get("y") != null && data.getExtras().get("address") != null) {
String sX = data.getExtras().get("x").toString();
String sY = data.getExtras().get("y").toString();
String sAddress = data.getExtras().get("address").toString();
double dX = Double.parseDouble(sX);
double dY = Double.parseDouble(sY);
ShowSearch(dX, dY, sAddress);
}
else{
Log.d("onActivityResult()", "Something went wrong, either the result code is wrong or the data is null");
}
}


Then on second thought, I opted for a try catch.

// Function to read the result from newly created activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100) {
try {
String sX = data.getExtras().get("x").toString();
String sY = data.getExtras().get("y").toString();
String sAddress = data.getExtras().get("address").toString();
double dX = Double.parseDouble(sX);
double dY = Double.parseDouble(sY);
ShowSearch(dX, dY, sAddress);
} catch (java.lang.NullPointerException e){
Log.d("onActivityResult()", "Something went wrong, some data is null");
}
}
}


But using a try catch brings back the warning in android-studio when I'm pretty sure it shouldn't because whether it's null or not, I'm handling it now.

Here's my question, which of the two solution is technically more efficient, if it's the try catch solution why does Android Studio keeps giving me a warning?

(Android Studio 2.1.1)

UPDATE: After trying multiple solutions, I realized that android studio gives me a warning even on the first example so I still have that warning but it's not bothering me anymore.

To those interested here's the solution I decided to use:

// Function to read the result from newly created activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100 && data != null) {
if (data.getExtras().get("x") != null && data.getExtras().get("y") != null
&& data.getExtras().get("address") != null) {
String sX = data.getExtras().get("x").toString();
String sY = data.getExtras().get("y").toString();
String sAddress = data.getExtras().get("address").toString();
double dX = Double.parseDouble(sX);
double dY = Double.parseDouble(sY);
ShowSearch(dX, dY, sAddress);
} else{
Log.d("onActivityResult()", "Something went wrong, some extra data is null");
}
} else{
Log.d("onActivityResult()", "Something went wrong, either the result code is wrong or the data is null");
}
}

Answer

You should not catch a NullPointerException - in fact very few RuntimeExceptions should be caught.

A NullPointerException denotes a problem with your code, where a variable is invoked methods upon (or its fields are accessed), while the reference actually has a null value.

This essentially mandates checking for null values.

That's where Android Studio seems to be pedantically proactive in this context: of course you may get NPEs by chaining method invocations on objects, and if you do not have a guarantee that the objects will not be null, you should check for null values.

For instance:

if (resultCode == 100 
    && data.getExtras().get("x") != null 
    && data.getExtras().get("y") != null 
    && data.getExtras().get("address") != null) { ...

... would become, tediously:

if (resultCode == 100 
    && data != null // unlikely
    && data.getExtras() != null
    && data.getExtras().get("x") != null 
    ...

... or rather, in this instance:

if (resultCode == 100 
    && data != null // unlikely
    && data.hasExtra("x")
    ...

That change is tedious and adds clutter, but it will hardly matter in terms of performance, as long as your method invocations are not mutating any object (otherwise, just assign to a variable before checking for null values).

There seem to be ways to parametrize Android Studio with regards to the warnings you get from the IDE.

See this question for the general direction.