How can I catch a NullPointerException from a method? Example inside

2.7k Views Asked by At

I am using the MapQuest jar in my Android app and I just came upon an issue with one of the methods required by the RouteManager.RouteCallback() that is a part of the MapQuest jar.

The method giving me the NullPointerException is the onError() method, as you will see in the code below. How can I catch a NullPointerException from that method so the app will not crash? I've tried wrapping the entire method in a try/catch but that doesn't work, it is giving me errors. Any suggestions? Thanks.

routeManager.setRouteCallback(new RouteManager.RouteCallback() {

        @Override
        public void onError(RouteResponse routeResponse) {
            Info info = routeResponse.info;
            int statusCode = info.statusCode;

            StringBuilder message = new StringBuilder();
            message.append("Unable to create route.\n").append("Error: ")
                    .append(statusCode).append("\n").append("Message: ")
                    .append(info.messages);
            Toast.makeText(getApplicationContext(), message.toString(),
                    Toast.LENGTH_LONG).show();
            createRouteButton.setEnabled(true);
        }

        @Override
        public void onSuccess(RouteResponse routeResponse) {
            clearButton.setVisibility(View.VISIBLE);
            if (showItineraryButton.getVisibility() == View.GONE
                    && showMapButton.getVisibility() == View.GONE) {
                showItineraryButton.setVisibility(View.VISIBLE);
            }
            createRouteButton.setEnabled(true);
        }

    });
}

Stack Trace:

01-11 14:26:51.296: E/AndroidRuntime(343): FATAL EXCEPTION: main
01-11 14:26:51.296: E/AndroidRuntime(343): java.lang.NullPointerException: println needs a message
01-11 14:26:51.296: E/AndroidRuntime(343):  at android.util.Log.println_native(Native Method)
01-11 14:26:51.296: E/AndroidRuntime(343):  at android.util.Log.e(Log.java:230)
01-11 14:26:51.296: E/AndroidRuntime(343):  at com.mapquest.android.maps.RouteManager.handleResponse(RouteManager.java:393)
01-11 14:26:51.296: E/AndroidRuntime(343):  at com.mapquest.android.maps.RouteManager.access$400(RouteManager.java:59)
01-11 14:26:51.296: E/AndroidRuntime(343):  at com.mapquest.android.maps.RouteManager$RouteTask.onPostExecute(RouteManager.java:648)
01-11 14:26:51.296: E/AndroidRuntime(343):  at com.mapquest.android.maps.RouteManager$RouteTask.onPostExecute(RouteManager.java:611)
01-11 14:26:51.296: E/AndroidRuntime(343):  at android.os.AsyncTask.finish(AsyncTask.java:417)
01-11 14:26:51.296: E/AndroidRuntime(343):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
01-11 14:26:51.296: E/AndroidRuntime(343):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
01-11 14:26:51.296: E/AndroidRuntime(343):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 14:26:51.296: E/AndroidRuntime(343):  at android.os.Looper.loop(Looper.java:123)
01-11 14:26:51.296: E/AndroidRuntime(343):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-11 14:26:51.296: E/AndroidRuntime(343):  at java.lang.reflect.Method.invokeNative(Native Method)
01-11 14:26:51.296: E/AndroidRuntime(343):  at java.lang.reflect.Method.invoke(Method.java:507)
01-11 14:26:51.296: E/AndroidRuntime(343):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-11 14:26:51.296: E/AndroidRuntime(343):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-11 14:26:51.296: E/AndroidRuntime(343):  at dalvik.system.NativeStart.main(Native Method)
3

There are 3 best solutions below

3
On

You don't catch NullPointerException. You simply don't do anything with null references.

Check which reference is null exactly, and check if (foo != null) {..}

3
On

It's generally not a good idea to catch NullPointerException. You should avoid dereferencing a null variable. For example checking that the variable is not null if you think it might be.

In your example it looks like createRouteButton is null for some reason.

1
On

Sometimes, we must admit defeat. You can either attempt to break open the jar with a sledgehammer or hang your head and post a thread at MapQuest's forums.

:(

http://developer.mapquest.com/web/products/forums/-/message_boards?_19_mbCategoryId=224714

Edit: Oh, it's worth a shot. Are you using the latest build of MPQ API?