Here is the code that is being called:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
context=this;
Timer timer=new Timer();
TimerTask task=new TimerTask() {
@Override
public void run() {
MyLocation myLocation=new MyLocation();
try {
MyLocation.getLocation(context, new MyLocationListener() {
@Override
public void gotLocation(Location location) {
SharedPreferences pref=getSharedPreferences(Utilities.USER_PREF, MODE_PRIVATE);
SharedPreferences.Editor editor=pref.edit();
double lat=location.getLatitude();
double lon=location.getLongitude();
editor.putString("lat", lat+"");
editor.putString("lon", lon+"");
Geocoder coder=new Geocoder(MyLocationService.this);
try {
List<Address> addresses=coder.getFromLocation(lat, lon, 1);
String address=addresses.get(0).getAddressLine(0)+","+addresses.get(0).getAdminArea();
editor.putString("address", address+"");
editor.commit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
editor.commit();
}
}
});
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
};
timer.schedule(task, 1000);
return Service.START_STICKY;
}
Here is the exception being thrown:
03-10 03:01:25.721: E/AndroidRuntime(912): FATAL EXCEPTION: Timer-0
03-10 03:01:25.721: E/AndroidRuntime(912): at com.barkleyandpaws.services.MyLocationService$1.run(MyLocationService.java:78)
03-10 03:01:25.721: E/AndroidRuntime(912): java.lang.NullPointerException
I'm just not sure what is going on ... I really appreciate any insight you might have. The intent of the code is to kick off a service & timer that gets periodic location information and stores it in variables that are used by other methods to display on maps or sort information by distance.
Here is the entire code for MyLocationService.java
package com.barkleyandpaws.services;
import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.IBinder;
import android.util.Log;
import com.barkleyandpaws.MyLocation;
import com.barkleyandpaws.MyLocation.MyLocationListener;
import com.barkleyandpaws.utils.Utilities;
public class MyLocationService extends Service {
Context context;
@Override
public void onDestroy() {
super.onDestroy();
}
// @Override
// public void onCreate() {
// //Log.i(XXX.LOG, "onCreate");
// super.onCreate();
// }
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
context=this;
Timer timer=new Timer();
TimerTask task=new TimerTask() {
@Override
public void run() {
MyLocation myLocation=new MyLocation();
try {
MyLocation.getLocation(context, new MyLocationListener() {
@Override
public void gotLocation(Location location) {
SharedPreferences pref=getSharedPreferences(Utilities.USER_PREF, MODE_PRIVATE);
SharedPreferences.Editor editor=pref.edit();
double lat=location.getLatitude();
double lon=location.getLongitude();
editor.putString("lat", lat+"");
editor.putString("lon", lon+"");
Geocoder coder=new Geocoder(MyLocationService.this);
try {
List<Address> addresses=coder.getFromLocation(lat, lon, 1);
String address=addresses.get(0).getAddressLine(0)+","+addresses.get(0).getAdminArea();
editor.putString("address", address+"");
editor.commit();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
editor.commit();
}
}
});
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
};
timer.schedule(task, 1000);
return Service.START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}