简体   繁体   中英

Android Timer - Java Null Pointer Exception

I have a Android app that is a basic timer with two buttons, start and stop. Whenever I press the stop button twice in a row, the app will crash with a null pointer exception.

The method in question is:

public void stopTimer(View view) {
timerTask.cancel();
timerTask = null;   
n = 0;
}

Logcat as follows:

01-27 20:43:01.173: E/dalvikvm(14195): could not disable core file generation for pid  
14195: Operation not permitted
01-27 20:43:32.455: D/AndroidRuntime(14195): Shutting down VM
01-27 20:43:32.455: W/dalvikvm(14195): threadid=1: thread exiting with uncaught exception (group=0x40018560)
01-27 20:43:32.478: E/AndroidRuntime(14195): FATAL EXCEPTION: main
01-27 20:43:32.478: E/AndroidRuntime(14195): java.lang.IllegalStateException: Could not execute method of the activity
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.view.View$1.onClick(View.java:2144)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.view.View.performClick(View.java:2485)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.view.View$PerformClick.run(View.java:9089)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.os.Handler.handleCallback(Handler.java:587)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.os.Looper.loop(Looper.java:123)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.app.ActivityThread.main(ActivityThread.java:3806)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at java.lang.reflect.Method.invokeNative(Native Method)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at java.lang.reflect.Method.invoke(Method.java:507)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at dalvik.system.NativeStart.main(Native Method)
01-27 20:43:32.478: E/AndroidRuntime(14195): Caused by: java.lang.reflect.InvocationTargetException
01-27 20:43:32.478: E/AndroidRuntime(14195):    at java.lang.reflect.Method.invokeNative(Native Method)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at java.lang.reflect.Method.invoke(Method.java:507)
01-27 20:43:32.478: E/AndroidRuntime(14195):    at android.view.View$1.onClick(View.java:2139)
01-27 20:43:32.478: E/AndroidRuntime(14195):    ... 11 more
01-27 20:43:32.478: E/AndroidRuntime(14195): Caused by: java.lang.NullPointerException
01-27 20:43:32.478: E/AndroidRuntime(14195):    at com.example.timer.MainActivity.stopTimer(MainActivity.java:56)
01-27 20:43:32.478: E/AndroidRuntime(14195):    ... 14 more
01-27 20:43:35.564: I/Process(14195): Sending signal. PID: 14195 SIG: 9
01-27 20:48:05.103: D/dalvikvm(14263): GC_EXPLICIT freed 75K, 51% free 2682K/5379K, external 2357K/2773K, paused 23ms

Is there anything I could do so that if the user happens to press the button twice in a row, the app won't be crashing on them? As it stands, if they press stop once the value of the int is still displayed. Maybe if stop was pressed again it could reset to 0?

Thank you in advance for any advice/feedback.

Looks like you need to check to make sure timerTask is not null before you call timerTask.cancel();

if (timerTask != null){
    timerTask.cancel();
    timerTask = null;
}

timerTask is null the second time. You can simply check if it's not already null:

public void stopTimer(View view) {
    if (timerTask != null) {
        timerTask.cancel();
        timerTask = null;   
    }
    n = 0;
}

Or you could disable/hide the button to prevent the user from clicking it twice (or apply both as checking for null is always a good habit).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM