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

  • SEARCH
  • Home
  • 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 4072352
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:52:26+00:00 2026-05-20T16:52:26+00:00

How to get the present location through GPS in android. I am using the

  • 0

How to get the present location through GPS in android. I am using the following code..

But when i am using LastKnowKnown location it is throwing null pointer exception.. Code is given below

Please any body help.. How to fix it?

public void getlocation()
    {       

        lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
        locationListener = new MyLocationListener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

    }

class MyLocationListener implements LocationListener 
 {
     String a;
     String b;
     String c;


    public void onLocationChanged(Location loc) 
    {
        if (loc != null) {

            LAT = loc.getLatitude();
            LONG = loc.getLongitude();  
            a=Double.toString(LAT);
            b=Double.toString(LONG);
            c="-----LAT is:"+a+"  "+"LONG is--------:"+b;
            System.out.println(c);



        }
    }

 public void getPresentLocation() {
        String a;
         String b;
         String c;

        lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);  
        LAT = location.getLatitude();
        LONG = location.getLongitude();
        a=Double.toString(LAT);
        b=Double.toString(LONG);
        c="-----LAT is:"+a+"  "+"LONG is--------:"+b;
        System.out.println(c);
  • 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-20T16:52:27+00:00Added an answer on May 20, 2026 at 4:52 pm

    From the documentation

    If the provider is currently disabled, null is returned.

    So if there is a possibility of the api returning null, keep null checks in place so avoid the exception. Just like you did in the first example.

    I think that there was no cached fix yet in your case.

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

Sidebar

Related Questions

I'm getting the following exception trying to pass an object through wcf: There was
I want to open a notepad file using VisualBasic.Interaction.Shell method. At present I get
I am currently doing this tutorial to get the GPS coordinates of my present
Visual Studio is not present at my work location and I am using Notepad++
Where to get the list of dlls present in microsoft word 12.0 . Can
i want to access or get the class which is present in different project.How
We get the following error; The request was aborted: Could not create SSL/TLS secure
I get the following error when running my Visual Studio 2008 ASP.NET project (start
I get the following error when building my Windows Forms solution: LC.exe exited with
I want to retrieve the latitude and longitude of the user's present location, I

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.