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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:59:20+00:00 2026-05-27T22:59:20+00:00

I am a newbie to android and async task. I am developing a application

  • 0

I am a newbie to android and async task.
I am developing a application where in a on click of a button a layout opens and searches for his device latitude and longitude.
These latitude and longitude are then sent to a server for getting back the details of branches from nearest to farthest.
I am getting an Null pointer Exception cant understand how AsyncTask is working as AsyncTask Execute never gets called. Below is the code

public class FindUs extends ExpandableListActivity implements OnChildClickListener{

GeoPoint geoPoint;
public static String cities[];
LocationManager mlocManager;
double lat,lng; 
LocationListener mlocListener = new MyLocationListener();
ProgressDialog progDailog = null;

RequestTask reqTask; 
SimpleExpandableListAdapter expListAdapter;

public String city_details[][] = {   };

    @Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    setContentView(R.layout.findus_layout);

progDailog = new ProgressDialog(FindUs.this);
    progDailog.setMessage("Loading...");

    progDailog.show();
   mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

try {
    reqTask = new RequestTask();
    reqTask.execute( "http://192.168.1.239:8080/StrutsExample/FindUS.slt");

    } catch (Exception e) {
        e.printStackTrace();
    }



 expListAdapter =
        new SimpleExpandableListAdapter(
            this,
            createGroupList(),// groupData describes the first-level entries
            R.layout.child_row, // Layout for the first-level entries
            new String[] { "cityBranch" },  // Key in the groupData maps to display
            new int[] { R.id.childname },       // Data under "child" key goes into this TextView
            createChildList(),  // childData describes second-level entries
            R.layout.child_row, // Layout for second-level entries
            new String[] { "cityBranchDetails", "rgb" },    // Keys in childData maps to display
            new int[] { R.id.childname, R.id.rgb }  // Data under the keys above go into these TextViews
        );

    setListAdapter( expListAdapter );


private List createGroupList() {

          ArrayList result = new ArrayList();
//getting null pointer exception when populating cities


for( int i = 0 ; i < cities.length ; ++i ) {
            HashMap m = new HashMap();
            m.put( "cityBranch",cities[i] );
            result.add( m );
          }
          return (List)result;
        }
 private List createChildList() {
        ArrayList result = new ArrayList();
        for( int i = 0 ; i < city_details.length ; ++i ) {
    // Second-level lists
          ArrayList secList = new ArrayList();
          for( int n = 0 ; n < city_details[i].length ; n += 2 ) {

            HashMap child = new HashMap();
            child.put( "cityBranchDetails", city_details[i][n] );
            child.put( "rgb", city_details[i][n+1] );

            secList.add( child );
          }
          result.add( secList );
        }
        return result;
      }


//Location Listener Class

 public class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location loc){

            double latitude = loc.getLatitude();

            double longitude = loc.getLongitude();

            String Text = "My current location is: " +  "Latitude = " + latitude +  "Longitude = " + longitude;

            Toast.makeText( FindUs.this,Text,   Toast.LENGTH_SHORT).show();

String latLongString = "";
        if (loc != null) {
            latLongString = "Lat:" + latitude + "\nLong:" + longitude;
            TextView myLocationText = (TextView)findViewById(R.id.FindUsTextView);
            myLocationText.setText("Your Current Position is:\n" + latLongString);

            progDailog.dismiss();

        } 

        Toast.makeText( FindUs.this, Text, Toast.LENGTH_SHORT).show();
        mlocManager.removeUpdates(mlocListener);

        }

        @Override

        public void onProviderDisabled(String provider){

        Toast.makeText( getApplicationContext(),"Gps Disabled, Please Enable the GPS", Toast.LENGTH_SHORT ).show();

        }

        @Override

        public void onProviderEnabled(String provider){

        Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();

        }

        @Override

        public void onStatusChanged(String provider, int status, Bundle extras){

        }
        }



// now the AsyncTask class

class RequestTask extends AsyncTask<String, String, String>{

            @Override
            protected String doInBackground(String... uri) {
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response;
                String responseString = null;

                try {
                    HttpPost requestDetails = new HttpPost(uri[0]);
                    SharedPreferences findUsSetDetailsSharedPreference = getSharedPreferences("gpsdetails",MODE_PRIVATE);
                    String latitude = findUsSetDetailsSharedPreference.getString("latitude", "");
                    String longitude = findUsSetDetailsSharedPreference.getString("longitude", "");
                    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                    postParameters.add(new BasicNameValuePair("latitude", latitude));
                    postParameters.add(new BasicNameValuePair("longitude", longitude));
                    requestDetails.setEntity(new UrlEncodedFormEntity(postParameters, HTTP.UTF_8));

                    response = httpclient.execute(requestDetails);
                    StatusLine statusLine = response.getStatusLine();
                    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        response.getEntity().writeTo(out);
                        out.close();
                        responseString = out.toString();
                        System.out.println("------------------------->"+responseString);
                    } else{
                        //Closes the connection.
                        response.getEntity().getContent().close();
                        throw new IOException(statusLine.getReasonPhrase());
                    }
                } catch (ClientProtocolException e) {
                    //TODO Handle problems..
                } catch (IOException e) {
                    //TODO Handle problems..
                }

                String []latandLong = responseString.split("\t");



/********************************************************

below we can see I am populating cities String array variables of FindUs
***********************************************************************************/

                FindUs.cities = new String[latandLong.length];
                for(int i=0 ; i<latandLong.length;i++)
                {
                    FindUs.cities[i] = latandLong[i];
                }           
                System.out.println(cities.length);
                System.out.println(cities);

                return responseString;
            }


 @Override
        protected void onDestroy() {
        super.onDestroy();
        if (mlocManager != null) {
            mlocManager.removeUpdates(mlocListener);
            mlocManager = null;
        }
        }

        @Override
        public void onConfigurationChanged(final Configuration newConfig)
        {
            // Ignore orientation change to keep activity from restarting
            super.onConfigurationChanged(newConfig);
        }



}


            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);

            }
        }

But it never gets reqTask.execute(); executed and doesn’t fill up the variable.
Looking forward to your reply.
thanks.

below is the stack trace

     01-04 19:29:42.759: E/AndroidRuntime(305): FATAL EXCEPTION: main
    01-04 19:29:42.759: E/AndroidRuntime(305): java.lang.RuntimeException: Unable to start activity 
    ComponentInfo{com.stylingandroid.IntelligentLayout/com.stylingandroid.IntelligentLayout.FindUs}: java.lang.NullPointerException

    01-04 19:29:42.759: E/AndroidRuntime(305):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

    01-04 19:29:42.759: E/AndroidRuntime(305):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

    01-04 19:29:42.759: E/AndroidRuntime(305):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)

    01-04 19:29:42.759: E/AndroidRuntime(305):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

    01-04 19:29:42.759: E/AndroidRuntime(305):  at android.os.Handler.dispatchMessage(Handler.java:99)

    01-04 19:29:42.759: E/AndroidRuntime(305):  at android.os.Looper.loop(Looper.java:123)

    01-04 19:29:42.759: E/AndroidRuntime(305):  at android.app.ActivityThread.main(ActivityThread.java:4627)

    01-04 19:29:42.759: E/AndroidRuntime(305):  at java.lang.reflect.Method.invokeNative(Native Method)

    01-04 19:29:42.759: E/AndroidRuntime(305):  at java.lang.reflect.Method.invoke(Method.java:521)

    01-04 19:29:42.759: E/AndroidRuntime(305):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

    01-04 19:29:42.759: E/AndroidRuntime(305):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

    01-04 19:29:42.759: E/AndroidRuntime(305):  at dalvik.system.NativeStart.main(Native Method)

    01-04 19:29:42.759: E/AndroidRuntime(305): Caused by: java.lang.NullPointerException

01-04 19:29:42.759: E/AndroidRuntime(305):  at 
`com.stylingandroid.IntelligentLayout.FindUs.createGroupList(FindUs.java:353)
01-04 19:29:42.759: E/AndroidRuntime(305):  at` 

    com.stylingandroid.IntelligentLayout.FindUs.onCreate(FindUs.java:200)
01-04 19:29:42.759: E/AndroidRuntime(305):  at 

`android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-04 19:29:42.759: E/AndroidRuntime(305):  at `


    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-04 19:29:42.759: E/AndroidRuntime(305):  ... 11 more

Exception always occurs at the function createGroupList for populating cities.

  • 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-27T22:59:20+00:00Added an answer on May 27, 2026 at 10:59 pm

    Hard to say exactly without seeing your code in one contiguous block but it looks like you are creating your adapter (which references cities) before your asynctask has completed. Therefore it is null and that’s why you are getting a null pointer even before your asynctask is executed.

    Why don’t you try initialising and assigning your adapter to the listview in the onPostExecute method of your asynctask…

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

Sidebar

Related Questions

I am an Android Newbie and I'm developing Go game application for android. I'm
I am newbie in android. I developed an android application that shows 15 tabs.
Hi am newbie to android development. I had created one application where i need
I am newbie in android. I had planned to develop an application which divides
I am newbie to android. I have client server based application. Server keeps on
I'm a newbie android developer, I'm trying to develop an application; a background service
I'm newbie in Android application development. I just downloaded all SDks and Eclispe. Then
I am newbie in android .i want to develop an application which has the
I am newbie in android application development. I am using the Form Stuff example
I'm rather newbie on Android, and I'm working on a simple application to get

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.