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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:55:04+00:00 2026-06-11T19:55:04+00:00

This question is continuation of my previous question. The link is https://stackoverflow.com/questions/12499941/android-getting-wrong-info-on-parsing-json-data#comment16829891_12499941 The problem

  • 0

This question is continuation of my previous question. The link is https://stackoverflow.com/questions/12499941/android-getting-wrong-info-on-parsing-json-data#comment16829891_12499941
The problem I am facing is, in doInBackground of Async Task, I am able to put data in NearLocation[]. Then, I passed this array to onPostExecute(). There I printed the data in NearLocation[], and it gave me the result whatever it had in array. This array, I am using in SitesOverlay class. Also, I am using this array in onCreate of activity to check whether it contains value. If not, it should give me an alert message. When I run my app, it showed NearLocation[] is null and gave me alert. But, at that time value was there in the array, I checked the same in onPostExecute. Now, I am not understanding, how the array became null, when it was having value in onPostExecute(). Did, i did anything wrong. I tried and searched a lot but couldn’t come up with solution. I am posting my updated code below as well as.

Activity class

public class TrackDriverActivity extends MapActivity {

Button btnCurrLoc;
EditText txtPickAddress;
MapView map;

MapController mc;
GeoPoint p;
boolean foundValidLocation = true;
public NearLocation[] nearLocations;
public String driverLatitude;
public String driverLongitude;
private MyLocationOverlay me=null;
private static final String TAG = "TrackDriverActivity";
String tripstatus;
/**
 * Nitish
 * Tue 11 Sep 2012 06:57 PM
 * */
private final String url = "http://towncarapp.com/android/near_driver.php?email="+ UserProfile.email;
JSONObject jsonObject;
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.trackdriver);

    Log.v(TAG,"Inside Pick Location Activity");
    new LoadDriverDetailsAsyncTask(TrackDriverActivity.this, url).execute();

    map = (MapView) findViewById(R.id.mapLocation);

    if(nearLocations!=null && nearLocations.length !=0)
    {
        for(int i = 0; i < nearLocations.length; i++)
        {
            NearLocation driverLocation = nearLocations[i];
            driverLatitude = driverLocation.lat;
            driverLatitude = driverLocation.lon;
            Log.d(TAG, "Latitude = " +driverLatitude);
            Log.d(TAG, "Latitude = " +driverLongitude);
        }//for
        Log.d(TAG, "onCreate nearLocation if");
    }//if
    else
    {
        TownCarDialogManager.showOkOnlyDialog(TrackDriverActivity.this, "Message", "No Driver assigned yet.");
        map.getController().setCenter(getPoint(39.83,-98.58));
        map.setBuiltInZoomControls(true);
        Log.d(TAG, "onCreate nearLocation else");
    }//else
    // First Get the current location
    Location currentLocation = getCurrentLocation();

    Log.v(TAG,"Till Current Location");
    if (currentLocation != null) 
    { 
        map.getController().setCenter(getPoint(currentLocation.getLatitude(), currentLocation.getLongitude()));

        Drawable marker=getResources().getDrawable(R.drawable.marker);
        marker.setBounds(0, 0, marker.getIntrinsicWidth(),
        marker.getIntrinsicHeight());

        try {
            map.getOverlays().add(new SitesOverlay(marker, getPoint(currentLocation.getLatitude(), currentLocation.getLongitude())));
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        map.invalidate();
    }//if 
    else 
    {
        Log.v("","No Current Location Found");
    }//else


    Drawable marker=getResources().getDrawable(R.drawable.marker2); //Driver Marker   
    marker.setBounds(0, 0, marker.getIntrinsicWidth(),
    marker.getIntrinsicHeight());

    try {
        map.getOverlays().add(new SitesOverlay(marker, null));
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



    map.getController().setZoom(17); 
    map.setBuiltInZoomControls(true);
    map.invalidate();

    /***/

    if (nearLocations == null || nearLocations.length <= 0) 
    {
        //showDialog("Sorry!!!","No driver has been assigned yet.",this);
        TownCarDialogManager.showOkOnlyPostProcessingDialog(TrackDriverActivity.this, "Message", "No Driver assigned yet.", 3);
        map.getController().setCenter(getPoint(39.83,-98.58));
        //map.getController().zoomToSpan(Integer.parseInt(nearLocations[0].lat),Integer.parseInt(nearLocations[0].lon));
        map.setBuiltInZoomControls(true);
    }//if


}//onCreate

@Override
protected boolean isRouteDisplayed() 
{
    // TODO Auto-generated method stub
    return false;
}//isRouteDisplayed

public Location getCurrentLocation()
{
    LocationManager locationManager = (LocationManager) TrackDriverActivity.this.getSystemService(Context.LOCATION_SERVICE);
    Criteria locCriteria = new Criteria();
    locCriteria.setAccuracy(Criteria.ACCURACY_FINE);
    Location lastLocation = locationManager.getLastKnownLocation(locationManager.getBestProvider(locCriteria, true));
    return lastLocation;        
}//getCurrentLocation

public String getCurrentLocationAddess()
{
    Location currentLocation = getCurrentLocation();
    String addressString = null;        

    if(currentLocation!=null)
    {
        Geocoder gc = new Geocoder(TrackDriverActivity.this, Locale.getDefault());
        try 
        {
            List<Address> addresses = gc.getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), 1);
            StringBuilder sb = new StringBuilder();

            if (addresses.size() > 0) 
            {
                Address address = addresses.get(0);

                for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                    sb.append(address.getAddressLine(i)).append("\n");

                sb.append(address.getCountryName());
            }//if
            addressString = sb.toString();
        }//try 
        catch (IOException e) 
        {
            Log.v("SelectPickupLocation","getCurrentLocationAddress::IOException ");
        }//catch
    }//if
    return addressString;
}//getCurrentLocationAddress

private double getDouble(String dbl){
    Double d;

    try{
        d = Double.valueOf(dbl);
    } catch (Exception e) {
        return 0;
    }

    System.out.println("***"+d);

    return d.doubleValue();
}

private GeoPoint getPoint(double lat, double lon) {
    return (new GeoPoint((int) (lat * 1000000.0), (int) (lon * 1000000.0)));
}   

private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
    private List<OverlayItem> items = new ArrayList<OverlayItem>();

    public SitesOverlay(Drawable marker, GeoPoint center) throws InterruptedException, ExecutionException {
        super(marker);
        boundCenterBottom(marker);

        if (center != null){
            items.add(new OverlayItem(center, "" , ""+getCurrentLocationAddess()));
        } else {
            //nearLocations = getNearLocations(jsonObject);
                if (nearLocations != null && nearLocations.length > 0 ) {
                Log.v("","+"+nearLocations.length);
                for (int i = 0; i < nearLocations.length; i++){
                    NearLocation loc = nearLocations[i];
                    Log.v(TAG,"*"+loc.lat+"*"+loc.lon);
                    items.add(new OverlayItem(getPoint(getDouble(driverLatitude), getDouble(driverLongitude)),"", "Driver Location"));                  
                }
            }
        }

        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return (items.get(i));
    }

    @Override
    protected boolean onTap(int i) {
        Toast.makeText(TrackDriverActivity.this, items.get(i).getSnippet(),
                Toast.LENGTH_SHORT).show();

        return (true);
    }

    @Override
    public int size() {
        return (items.size());
    }
}

private class LoadDriverDetailsAsyncTask extends AsyncTask<String, Void, NearLocation[]>
{
    private ProgressDialog pd;
    Context ctx;
    String url;
    public LoadDriverDetailsAsyncTask(Context ctx, String url)
    {
        this.ctx = ctx;
        this.url = url;
    }//Constructor

    protected void onPreExecute() 
    {
        super.onPreExecute();

        pd=new ProgressDialog(ctx);
        pd.setMessage("Please Wait...");
        pd.setIndeterminate(true);
        pd.setCancelable(false);
        pd.show();
    }//onPreExecute

    protected NearLocation[] doInBackground(String... params) 
    {
        JSONObject jObject = JSONParser.getJSONObjectDataFromURL(url);  
        nearLocations = getNearLocations(jObject);
        return nearLocations;
    }//doInBackground

    protected void onPostExecute(NearLocation[] nearLocations) 
    {
        for(int i = 0; i < nearLocations.length; i++){
            NearLocation loc = nearLocations[i];
            Log.d("LoadDriverAsyncTaskDriverlATITUDE", loc.lat);
            Log.d("LoadDriverAsyncTaskDriverlongitude", loc.lon);
        }
        pd.dismiss();
    }//onPostExecute
}//LoadMapAsyncTask

public NearLocation[] getNearLocations(JSONObject jObject) 
{
    NearLocation[] nearLocation = null;

    try
    {
        if(jObject!=null)
        {
            JSONArray jsonArray = jObject.getJSONArray("statement");

            if (jsonArray != null) 
            {
                nearLocation = new NearLocation[jsonArray.length()];
                Log.v(TAG, "::::::&&&&&&&&&&&&&::::::NearLocations "
                    + nearLocation.length);

                for (int i = 0; i < jsonArray.length(); i++) 
                {
                    JSONObject e = jsonArray.getJSONObject(i);
                    NearLocation loc = new NearLocation(
                            e.getString("latitude"),
                            e.getString("longitude"));
                    nearLocation[i] = loc;
                    Log.v("Driver latitude:", loc.lat);
                    Log.v("Driver longitude:", loc.lon);
                }//for
            }//if
        }//if
        else
        {
            TownCarDialogManager.showOkOnlyDialog(TrackDriverActivity.this, "Sorry", "There is problem in internet connection");
        }//else
    }//try 
    catch (JSONException e) 
    {
        e.printStackTrace();
    }//catch

    return nearLocation;
}//getNearLocations

}
  • 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-11T19:55:05+00:00Added an answer on June 11, 2026 at 7:55 pm

    I’m not sure I understand the problem you have, but here are some issues with your code, see if they solve the problem. They way you setup the code(especially the AsyncTask) might work in some cases but it will fail in most. In the onCreate method you instantiate the task and then you call execute() to start it. At this moment the code in the onCreate method will continue to be run(and the task will do the same), but as the task most likely hasn’t finished getting the data, the nearlocations array will be null. So the problem is that you don’t wait to let the task to finish getting the data so you end up using the null nearLocations variable. One way to avoid this is to use a callback system. Below is an example:

    // this is an interface that the TrackDriverActivity will implement
    interface OnLoadDriverDetails {
         onLoadDriverDetails(NearLocation[] data); 
    }
    

    this callback will be called from the AsyncTask when it finishes loading the data:

    private class LoadDriverDetailsAsyncTask extends AsyncTask<String, Void, NearLocation[]> {
    
        OnLoadDriverDetails mListener;
    
        public LoadDriverDetailsAsyncTask(Context ctx, String url) {
            this.ctx = ctx;
            this.url = url;
            mListener = (OnLoadDriverDetails) ctx;
        }
    
        protected NearLocation[] doInBackground(String... params) {
            JSONObject jObject = JSONParser.getJSONObjectDataFromURL(url);          
            return getNearLocations(jObject);
        }
    
        protected void onPostExecute(NearLocation[] nearLocations) {
             mListener.onLoadDriverDetails(newrLocations);
        }
        // the rest of the task's code
    

    In the activity you’ll move the code from onCreate that needs the valid nearLocations array in the onLoadDriverDetails callback:

    public class TrackDriverActivity extends MapActivity implements OnLoadDriverDetails {
    
        // ...
        public void onLoadDriverDetails(NearLocation[] data) {
            nearLocations = data;
            // here do the things you do in the onCreate method like setting those SitesOverlay items on the map
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As a continuation of my previous question in stackoverflow: Getting LINK error : Extern
This is a continuation from a previous stackoverflow question. I've renamed some variables so
This is a continuation from the previous stackoverflow jQuery question I asked, but I
This question is a continuation of my previous question here zend models architecture (big
This question is in continuation to my previous question . I am trying to
This is a continuation of my previous question .NET regex engine returns no matches
This question is a continuation of a previous thread to compare two lists with
This question is in continuation to my previous question, in which I asked about
This is in continuation to my previous question which was closed cause of similarity
This is a continuation of my previous question which I posted when I wasn't

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.