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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:41:02+00:00 2026-05-26T22:41:02+00:00

I am using AsyncTask class and as soon as i start the app I

  • 0

I am using AsyncTask class and as soon as i start the app I am getting null pointer exception and app goes Forceclose.In this Activity i want to parse the json string and load the data in listview.

here is the Activiy.

public class JsonExampleActivity extends ListActivity {
 /** Called when the activity is first created. */
 ProgressDialog mDialog;
 Context mContext;
 Location location;
 LocationManager lm;
 final String TAG="a.c.b";
 JSONFunction JSONfunction;
 double latitude[]=new double[20];
 double longitude[]=new double[20];
 String reference[]=new String[20];
 double distance[]=new double[20];
 final Intent intent=new Intent(this ,GetLatAndLng.class);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.listplaceholder);

    new JSONPasingShowList().execute();
}

public void updateWithNewLocation(Location location2) {
    if(location2!=null) {
          double geoLat = location2.getLatitude();

            double geoLng = location2.getLongitude();
    }   
}

class JSONPasingShowList extends AsyncTask<Void, Void,Void>
{  

    @Override
    protected void onPreExecute() {
        mDialog = ProgressDialog.show(mContext,"Loading","Please wait...", true);
    }

@Override
protected Void doInBackground(Void... params) {

    LocationManager locationManager;
    String context=Context.LOCATION_SERVICE;
    locationManager=(LocationManager)getSystemService(context);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(provider);

    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
        updateWithNewLocation(location);
        }
        public void onProviderDisabled(String provider){
        updateWithNewLocation(null);
        }
        public void onProviderEnabled(String provider){ }
        public void onStatusChanged(String provider, int status,
        Bundle extras){ }
        };

    updateWithNewLocation(location);
    locationManager.requestLocationUpdates(provider, 2000, 10,
            locationListener);


    double geoLat = location.getLatitude();
    Log.v(TAG, "hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"+geoLat);
    double geoLng = location.getLongitude();
    Log.v(TAG, "hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"+geoLng);


    JSONObject  json=JSONFunction.getJSONfromURL("https://maps.googleapis.com/maps/api/place/search/json?location=37.422006,-122.084095&radius=1000&types=doctor&sensor=true&key=AIzaSyA1kh_VA6cnhwHrXA0TAFTba5Kt81dZKzc");

    try{
        JSONArray  JArray = json.getJSONArray("results");
           Log.v(TAG, "getting results");
        for(int i=0;i<JArray.length();i++){                     
            HashMap<String, String> map = new HashMap<String, String>();    
            JSONObject e = JArray.getJSONObject(i);
            JSONObject location1=e.getJSONObject("geometry").getJSONObject("location");
            latitude[i]=location1.getDouble("lat");
            longitude[i]=location1.getDouble("lng");
            reference[i]=e.getString("reference");
            Log.v(TAG, reference[i]);
            distance[i]=GetLatAndLng.gps2m(geoLat, geoLng,latitude[i] ,longitude[i]); 

            map.put("id",  String.valueOf(i));
            map.put("name", "" + e.getString("name"));
            map.put("vicinity", "Address " +  e.getString("vicinity")+" "+"Disance:"+distance[i]);

            mylist.add(map);                        
        }   }catch(JSONException e)        {
             Log.e("log_tag", "Error parsing data "+e.toString());
        }
        Bundle b=new Bundle();
        b.putStringArray("key", reference);
        intent.putExtras(b);
         ListAdapter adapter = new SimpleAdapter(JsonExampleActivity.this, mylist , R.layout.listview, 
                 new String[] { "name", "vicinity", }, 
                 new int[] { R.id.item_title, R.id.item_subtitle });

 setListAdapter(adapter);

 final ListView lv = getListView();
 lv.setTextFilterEnabled(true); 
 lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
        @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
       Toast.makeText(JsonExampleActivity.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
        intent.putExtra("clickedid",position);
        startActivity(intent);


        }
    });

    return null;
}
@Override
protected void onPostExecute(Void result) {
    mDialog.dismiss();


}

}
 } 

LogCat:

 11-17 20:29:54.186: ERROR/AndroidRuntime(379): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.where/com.where.JsonExampleActivity}: java.lang.NullPointerException
 11-17 20:33:09.487: ERROR/AndroidRuntime(416): Uncaught handler: thread main exiting due to uncaught exception
 11-17 20:33:09.527: ERROR/AndroidRuntime(416): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.where/com.where.JsonExampleActivity}: java.lang.NullPointerException
 11-17 20:33:09.527: ERROR/AndroidRuntime(416):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
 11-17 20:33:09.527: ERROR/AndroidRuntime(416):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
 11-17 20:33:09.527: ERROR/AndroidRuntime(416):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
 11-17 20:33:09.527: ERROR/AndroidRuntime(416):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
 11-17 20:33:09.527: ERROR/AndroidRuntime(416):     at android.os.Handler.dispatchMessage(Handler.java:99)
 11-17 20:33:09.527: ERROR/AndroidRuntime(416):     at android.os.Looper.loop(Looper.java:123)
 11-17 20:33:09.527: ERROR/AndroidRuntime(416):     at android.app.ActivityThread.main(ActivityThread.java:4363)
 11-17 20:33:09.527: ERROR/AndroidRuntime(416):     at java.lang.reflect.Method.invokeNative(Native Method)
 11-17 20:33:09.527: ERROR/AndroidRuntime(416):     at java.lang.reflect.Method.invoke(Method.java:521) 
 11-17 20:33:09.527: ERROR/AndroidRuntime(416):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
 11-17 20:33:09.527: ERROR/AndroidRuntime(416):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
 11-17 20:33:09.527: ERROR/AndroidRuntime(416):     at dalvik.system.NativeStart.main(Native Method)
 11-17 20:33:09.527: ERROR/AndroidRuntime(416): Caused by: java.lang.NullPointerException

Any help would be appreciated.
Thanks in Advance!!

  • 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-26T22:41:03+00:00Added an answer on May 26, 2026 at 10:41 pm

    You don’t seem to be setting mContext anywhere.

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

Sidebar

Related Questions

I am using the following code class AddTask extends AsyncTask<Void, Void, Void> { ProgressDialog
I am using this code (inside an AsyncTask) to download files: URL u =
I do have a problem working with my application. I am using AsyncTask class
I'm using an AsyncTask class to add, remove and clear items from my ArrayAdapter
I am using AsyncTask to get data from a server and want to show
I'm using AsyncTask to download an image and want to send the downloaded image
I'm using AsyncTask in my custom Dialog. AsyncTask works fine in activity, but inside
How to create and update listview using AsyncTask getting data from server.Actually i am
I'm using this tutorial on AsyncTask with a task and a notification: https://eliasbland.wordpress.com/2011/03/11/an-example-of-how-to-run-a-background-task-and-report-progress-in-the-status-bar-using-asynctask-on-android/ What
In my app i have a processDialog (using AsyncTask) and at doInbackground it fetches

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.