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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:49:59+00:00 2026-06-07T05:49:59+00:00

This is my first Android App. I’m trying to call a function in a

  • 0

This is my first Android App. I’m trying to call a function in a service from a different class i am using foursquare API. I tried this function earlier in an activity and it worked perfectly but in a service im getting a NullPointerException.
This is the code i am using:

public class GuideMeService extends Service implements LocationListener {

LocationManager locationManager;
Geocoder geocoder;
// private CalendarContentResolver Calendar;
public FoursquareApp mFsqApp;
public ArrayList<FsqVenue> mNearbyList;
private static String TAG = "Service Class";
double lat;
double lng;
public static final String[] FIELDS = { CalendarContract.Calendars.NAME,
        CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
        CalendarContract.Calendars.CALENDAR_COLOR,
        CalendarContract.Calendars.VISIBLE };

public static ArrayList<String> Events = new ArrayList<String>();
public static final Uri CALENDAR_URI = Uri
        .parse("content://com.android.calendar/calendars");
public static final Uri EVENTS_URI = Uri
        .parse("content://com.android.calendar/events");
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in  Milliseconds
public static String query = "";

Set<String> calendars = new HashSet<String>();

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId); 
    Log.d(TAG, "GuideMe Servise started");
    //this.stopSelf();
    locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            MINIMUM_TIME_BETWEEN_UPDATES,
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            new MyLocationListener()
            );

    Events = readCalendarEvent(getApplicationContext());
    for(int i = 0 ; i < Events.size() ; i++){
        query += Events.toArray()[i].toString() + " ";
    }
    Thread thread = new Thread()
    {
          @Override
          public void run() {

                try {
                        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        mNearbyList = mFsqApp.SearchBykeyword(location.getLatitude(), location.getLongitude(), query);
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
          }
  };

thread.start();

}

Im getting The error on this line :

mNearbyList = mFsqApp.SearchBykeyword(location.getLatitude(), location.getLongitude(), query);

And this is the function i’m calling in the mFsqApp class:

public ArrayList<FsqVenue> SearchBykeyword(double latitude, double longitude, String query) throws Exception {
    ArrayList<FsqVenue> venueList = new ArrayList<FsqVenue>();
    try {
        String ll   = String.valueOf(latitude) + "," + String.valueOf(longitude);
        URL url     = new URL(API_URL + "/venues/search?ll=" + ll + "&query=" + query + "&radius=" + 50 + "&oauth_token=" + mAccessToken + "&v=20120610");

        Log.d(TAG, "Opening URL " + url.toString());

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setRequestMethod("GET");
        urlConnection.setDoInput(true);
        //urlConnection.setDoOutput(true);

        urlConnection.connect();
        String response     = streamToString(urlConnection.getInputStream());
        JSONObject jsonObj  = (JSONObject) new JSONTokener(response).nextValue();

        JSONArray groups    = (JSONArray) jsonObj.getJSONObject("response").getJSONArray("groups");

        int length          = groups.length();
        if (length > 0) {
            for (int i = 0; i < length; i++) {
                JSONObject group    = (JSONObject) groups.get(i);
                JSONArray items     = (JSONArray) group.getJSONArray("items");

                int ilength         = items.length();

                for (int j = 0; j < ilength; j++) {
                    JSONObject item = (JSONObject) items.get(j);

                    FsqVenue venue  = new FsqVenue();

                    venue.id        = item.getString("id");
                    venue.name      = item.getString("name");

                    JSONObject location = (JSONObject) item.getJSONObject("location");

                    Location loc    = new Location(LocationManager.GPS_PROVIDER);

                    loc.setLatitude(Double.valueOf(location.getString("lat")));
                    loc.setLongitude(Double.valueOf(location.getString("lng")));

                    venue.location  = loc;
                    //venue.address = location.getString("address");
                    venue.distance  = location.getInt("distance");
                    //venue.herenow = item.getJSONObject("hereNow").getInt("count");
                    venue.type      = group.getString("type");

                    venueList.add(venue);
                }
            }
        }
    } catch (Exception ex) {
        throw ex;
    }
    return venueList;
}

Updates:

07-08 21:26:18.580: W/System.err(24365): java.lang.NullPointerException
07-08 21:26:18.580: W/System.err(24365):    at com.android.guideme.GuideMeService$1.run(GuideMeService.java:86)
  • 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-07T05:50:00+00:00Added an answer on June 7, 2026 at 5:50 am

    You’re declaring the (unfortunately public) mFsqApp variable here:

    public FoursquareApp mFsqApp;
    

    but you haven’t shown any code to assign it a value – so it will have the default value of null, causing a NullPointerException when you dereference it. You need to assign a non-null value to it before you dereference it, e.g. with

    mFsqApp = new FoursquareApp();
    

    … or using a value passed in elsewhere (e.g. to the constructor). It’s hard to tell which of those is appropriate in this case, but something has to assign a non-null value to it. You say similar code worked earlier in an activity – so look back at that code and see where the value was coming from there.

    (Then improve your code to avoid public variables, avoid static variables where possible, avoid catching Exception, avoid just continuing in the face of an exception unless you’re really handled it etc.)

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

Sidebar

Related Questions

This is my first Android app and I've encountered an exception when trying to
I have published my first android app and its quite different from iPhone. On
this is my first Android app and I'm trying to use Sockets to send
I am trying to get my first Android service to work. I have this
Bear with me, as this is my first Android app. :) Essentially, I would
How to create in-app step by step instructions? Like this ( first boot android
Up front: This is my first attempt at an Android app. I'm in that
This is my first android project so please help I am trying to create
Using Dozer to map two objects, I have: /** /* This first class uses
This is my first Android app. I need to email what I have so

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.