-1

I use a GPSTracker class, with the method OnLocationChanged I want to send a notification when ....the locaiton change :)

i use :

@Override
public void onLocationChanged(Location location) {
    latitude = (double) (location.getLatitude());
    longitude = (double) (location.getLongitude());
    //   Toast.makeText(this, "fffff", Toast.LENGTH_LONG).show();
    //  Toast.makeText(getApplicationContext(), Double.toString(latitude)   , Toast.LENGTH_LONG).show();

    Log.i("okok", "position changee" + Double.toString(latitude) + "-" + Double.toString(longitude));


        NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notify = new Notification(R.drawable.a, "tittle", System.currentTimeMillis());
        PendingIntent pending = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);

        notify.setLatestEventInfo(getApplicationContext(), "subject", "body", pending);
        notif.notify(0, notify);

}

and there is an error on the 1st line of the NotificationLManager: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

Same issue when trying to use an HttpConnection to a remote website. I tryied already to use the try{} method

Would you have an idea pleaaaaase thanks David

  • can you put here you full class code – Anand Kumar Jha May 06 '16 at 06:24
  • 1
    GPSTracker? There is no such class in Android's SDK... Are you talking about extremely wrong written class from androidhive's so called tutorial? For your sanity, please, don't use it.. – Selvin May 06 '16 at 06:51
  • The answers given so far are spot on and there's also [this useful discussion](http://stackoverflow.com/questions/7298731/when-to-call-activity-context-or-application-context) about the different ways to get a `Context` and which one to use in which use case. – Markus Kauppinen May 06 '16 at 08:10
  • why one should I use ? – david marchioni May 07 '16 at 11:12

2 Answers2

1

I think your getting this error because your Context is null

you can try to wrap it like this and debug

if (getApplicationContext() != null) {
// Code goes here.
}

it has to do something with with your Context, try to access it different

it would have been better if you would have posted logcat

Max
  • 12,408
  • 4
  • 48
  • 62
  • public void onLocationChanged(Location location) { latitude = (double) (location.getLatitude()); longitude = (double) (location.getLongitude()); if (getApplicationContext() != null) { Toast.makeText(this, "fffff", Toast.LENGTH_LONG).show(); // Toast.makeText(getApplicationContext(), Double.toString(latitude) , Toast.LENGTH_LONG).show(); /* long now = location.getTime(); Log.i("gps", "onLocationChanged. gps time: " + now); notifBuilder.setContentText(now + "," + now ); getNotifMngr().notify(1, notifBuilder.getNotification());*/ } – david marchioni May 07 '16 at 11:13
1
getSystemService(java.lang.String)

is a method of Context class. So it need a Context object to be called from. What is your GPSTracker class? Is it subclass Context class i.e. Activity, application, Service? If not call this method with a proper context object which can't be null at a time of calling it.

Your complete code for GPSTracker class help to spot the error in particular, otherwise you can identify it yourself.

Mahendra Chhimwal
  • 1,792
  • 5
  • 18
  • 32