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 NEW solution I decided to use: (I still get warnings)
// 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.hasExtra("x") && data.hasExtra("y") && data.hasExtra("address")) { 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 { Toast.makeText(this, "Error in location", Toast.LENGTH_SHORT).show(); Log.d("onActivityResult()", "Something went wrong, some extra data is null"); } } else { Toast.makeText(this, "Error in location", Toast.LENGTH_SHORT).show(); Log.d("onActivityResult()", "Something went wrong, some extra data doesn't exist"); } } else{ Toast.makeText(this, "No Location found", Toast.LENGTH_SHORT).show(); Log.d("onActivityResult()", "Something went wrong, either the result code is wrong or the data is null"); } }
getExtras()can returnnull. Use ofIntent#hasExtra(String name)would solve that, idk if the nullcheck is aware of that though.