2

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;
    }


}
  • 3
    Which line is line 78? – A--C Mar 10 '13 at 03:20
  • its always best to post full stack trace. Also like A_C said what line is line 78 in MyLocationService.java(can you post the whole class?), that is where there is a null pointer. – Mark Mar 10 '13 at 03:31
  • Line 78 In the code is: e.printStackTrace(); – user2141555 Mar 10 '13 at 03:32
  • 1
    @user2141555 `e` is probably not the `null` reference, it is an Exception, which should be instantiated already. Rerun your program, and reasses the line that the NPE is printed on. – A--C Mar 10 '13 at 03:35
  • Thanks everyone for all your help ... does this catlog help: 03-10 03:29:26.054: E/AndroidRuntime(909): FATAL EXCEPTION: Timer-0 03-10 03:29:26.054: E/AndroidRuntime(909): java.lang.NullPointerException 03-10 03:29:26.054: E/AndroidRuntime(909): at com.barkleyandpaws.services.MyLocationService$1.run(MyLocationService.java:78) 03-10 03:29:26.054: E/AndroidRuntime(909): at java.util.Timer$TimerImpl.run(Timer.java:284) – user2141555 Mar 10 '13 at 03:43
  • The entire code for the class is posted above also... – user2141555 Mar 10 '13 at 03:48
  • @user2141555 the Exception shouldn't be null, try [cleaning your project and rerunning](http://stackoverflow.com/questions/7755364/catching-null-exception). – A--C Mar 10 '13 at 03:52
  • Here is the trace after I cleaned the project 03-10 03:56:04.681: E/AndroidRuntime(912): FATAL EXCEPTION: Timer-0 03-10 03:56:04.681: E/AndroidRuntime(912): java.lang.NullPointerException 03-10 03:56:04.681: E/AndroidRuntime(912): at com.barkleyandpaws.services.MyLocationService$1.run(MyLocationService.java:78) 03-10 03:56:04.681: E/AndroidRuntime(912): at java.util.Timer$TimerImpl.run(Timer.java:284) – user2141555 Mar 10 '13 at 03:58

0 Answers0