FATAL EXCEPTION: Timer-0 java.lang.NullPointerExce

2019-07-30 05:11发布

问题:

I'm having a timer in the oncreate() like:

final Timer timer1 = new Timer();
timer1.schedule(new TimerTask() {
    public void run() {
        sendSMS();
    }
}, 20000);

The methode sendSMS() is like:

private void sendSMS(){
    float test2;
    test2 = reallocation.getAccuracy(); //-> This is the line where logcat says.... NullPointerException
    ....
}

The logcat says:

05-30 18:32:26.962: W/dalvikvm(741): threadid=9: thread exiting with uncaught exception (group=0x4001d5a0)
05-30 18:32:26.972: E/AndroidRuntime(741): FATAL EXCEPTION: Timer-0
05-30 18:32:26.972: E/AndroidRuntime(741): java.lang.NullPointerException
05-30 18:32:26.972: E/AndroidRuntime(741):  at com.eljoom.df.ActionGPS.sendSMS(ActionGPS.java:280)

Well after searching the net I believe I need a handler for the timer... but I have no clue how to implement it.

Does anybody can put me on the right track?!

回答1:

reallocation is uninitialized, so its taking the null value, the default one for object.



回答2:

I set reallocation as Location: Location reallocation=null; – user1404924 3 mins ago

you set reallocation =null. you can not invoke reallocation's method reallocation.getAccuracy(); because you dont have a instance of the obj yet.

I suggest you to take a look at LocationManager class. You can request location from there with a specific location source http://developer.android.com/reference/android/location/LocationManager.html