Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3488854
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:19:21+00:00 2026-05-18T11:19:21+00:00

This is actually two questions: The first relates to my program The second is

  • 0

This is actually two questions:

  1. The first relates to my program
  2. The second is a more general question regarding debugging.

In my code, I have two button listeners. When the first is clicked, the onClickListener creates the locationlistener as well as the LocationManager which requests updates from the locationlistener. (See code below.)

LocationManager locationManager = (LocationManager)  getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationlistener);

On the click of the second button, I try to unregister the listener as follows:

locationManager.removeUpdates(locationlistener);

However, I end up with a force close and the following info in the debugger:

ERROR/AndroidRuntime(310): FATAL EXCEPTION: main
ERROR/AndroidRuntime(310): java.lang.NullPointerException
ERROR/AndroidRuntime(310):     at com.ryan.gcal.GRunCal$2.onClick(GRunCal.java:94)
ERROR/AndroidRuntime(310):     at android.view.View.performClick(View.java:2408)
ERROR/AndroidRuntime(310):     at android.view.View$PerformClick.run(View.java:8816)
ERROR/AndroidRuntime(310):     at android.os.Handler.handleCallback(Handler.java:587)
ERROR/AndroidRuntime(310):     at android.os.Handler.dispatchMessage(Handler.java:92)
ERROR/AndroidRuntime(310):     at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(310):     at android.app.ActivityThread.main(ActivityThread.java:4627)
ERROR/AndroidRuntime(310):     at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(310):     at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(310):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
ERROR/AndroidRuntime(310):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
ERROR/AndroidRuntime(310):     at dalvik.system.NativeStart.main(Native Method)

Line 94 of my code, where the error originates, is where I tried to remove the location listener. So, I assume I am not removing the location listener properly, but I am not sure what is wrong. My LocationManager is declared at the start of my code:

Locationmanager locationManager;

My first question is, what am I doing wrong in removing the listener? And secondly, can anyone provide some tips/advice on learning to interpret debug errors meaningfully?
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:19:21+00:00Added an answer on May 18, 2026 at 11:19 am

    You need to either:

    A) define the locationManager instance as a class variable, or retrieve it again in your onclick.

    LocationManager locationManager;
    
    public void onClick1()
    {
        locationManager = (LocationManager)  getSystemService(Context.LOCATION_SERVICE);
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationlistener);
    
    }
    
    public void onClick2()
    {
        locationManager.removeUpdates(locationlistener);
    }
    

    You need to make sure that in onClick1() instead of

    LocationManager locationManager = (LocationManager)  getSystemService(Context.LOCATION_SERVICE);
    

    you put

    locationManager = (LocationManager)  getSystemService(Context.LOCATION_SERVICE);
    

    Specifying LocationManager locationManager makes the scope of your declaration enclosed to the method its assign in, rather than in the scope of the class. This means in your 2nd click locationManager is infact null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is actually a two part question. First,does the HttpContext.Current correspond to the current
Actually, this question seems to have two parts: How to implement pattern matching? How
This is actually two questions in one: First, when you write your XAML and
I actually have two questions regarding the same problem but I think it is
This is a two part question, but I want to make sure the first
What are good ways of achieving this DB agnosticism without actually coding two DAL's?
I'm pretty new to the idea of recursion and this is actually my first
Disclaimer: This is not actually a programming question, but I feel the audience on
Does anyone actually think this is a good reason to Dumb down your code?
I've looked at every question so far and none seem to actually answer this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.