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 8539587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:24:03+00:00 2026-06-11T11:24:03+00:00

In my app, I have to find whether a given location comes under a

  • 0

In my app, I have to find whether a given location comes under a specified area. I am taking Connaught Place, New Delhi as center point. and I got the addresses which come under area of 200 miles from center point. But, if I enter any invalid location, like “abcdfdfkc”, the app crashes, because it is trying to find coordinates of this location and I want to avoid this.

Below I am posting the code:

public static  boolean isServicedLocation(Context _ctx, String strAddress){
    boolean isServicedLocation = false;

    Address sourceAddress = getAddress(_ctx, "Connaught Place, New Delhi, India");
    Location sourceLocation = new Location("");
    sourceLocation.setLatitude(sourceAddress.getLatitude());
    sourceLocation.setLongitude(sourceAddress.getLongitude());      

    Address targetAddress = getAddress(_ctx, strAddress);
    Location targetLocation = new Location("");

    if (targetLocation != null) {
        targetLocation.setLatitude(targetAddress.getLatitude());
        targetLocation.setLongitude(targetAddress.getLongitude());
        float distance = Math.abs(sourceLocation.distanceTo(targetLocation));
        double distanceMiles = distance/1609.34;
        isServicedLocation = distanceMiles <= 200;

        //Toast.makeText(_ctx, "Distance "+distanceMiles, Toast.LENGTH_LONG).show();
    }       

    return isServicedLocation;
}

getAddress method:

public static Address getAddress(Context _ctx, String addressStr) {
    Geocoder geoCoder = new Geocoder(_ctx, Locale.getDefault());
    try {
        List<Address> addresses = geoCoder.getFromLocationName(addressStr,
                1);

        if (addresses.size() != 0) {
            return addresses.get(0);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}
  • 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-06-11T11:24:05+00:00Added an answer on June 11, 2026 at 11:24 am

    It’s because when you don’t find an address from the GeoCoder (ie, if addresses.size() == 0), you return null.

    Then, regardless of that, you dereference the value, which is what’s crashing your app.

    Address targetAddress = getAddress(_ctx, strAddress);
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    :
    if (targetLocation != null) {
        targetLocation.setLatitude(targetAddress.getLatitude());
                                   ^^^^^^^^^^^^^
    

    You should probably also be checking targetAddress for null to avoid this (either in addition to (likely), or instead of (less likely), the check of targetLocation).

    So I’d be looking at changing:

    if (targetLocation != null) {
    

    into:

    if ((targetLocation != null) && (targetAddress != null)) {
    

    That way, an invalid address automatically becomes an unserviced location.

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

Sidebar

Related Questions

I am working on a little WinForm app and have been trying to find
I have this code App.Model('users').find(function (err, users) { users.forEach(function(user) { console.log(user.username); }); }); //make
i have uploaded an app yesterday by 6pm but till now i can't find
I have implemented Facebook into my app but now I find that whenever I
I have this line: @users = database['users'].find(:all).limit(10) it returns this object: <Mongo::Cursor:0x8759a858 namespace='app-development.users' @selector=:all
I cannot get my Spring web app to find my scripts. I have the
Within an asp.net MVC app, I'm trying to find rooms which have a guest/client,
I need to have my app monitor whether the GPS adapter is anabled or
I've started to wondering whether ASP.NET Webforms/MVC even have a place in the web
I'm trying to find out whether Appcelerator's Titanium is good for iPad app development,

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.