1

I'm getting a NullPointerException in one of my applications. I had thought I was accounting for all places in this piece of code for Null references, but it seems that I may have missed something. I've looked the code through and through, but I'm still not sure where this NullPointerException is being thrown from.

This is an error reported by a user, so I can't debug, nor have I ever gotten this error while in development

What's happening in the code/application:

The user starts up the application and I attempt to get their last known location either by NETWORK or GPS. Once I set the location, if available, I later on get weather for that location and display it to the user.

Does anybody have any idea where in this code this could be failing? Is there some further checks for GPS/NETWORK that I should be doing? Any help is much appreciated!

Stack Trace

java.lang.NullPointerException
at com.hookedroid.fishingcompanion.BaseActivity.getLastKnownLocation(BaseActivity.java:337)
at com.hookedroid.fishingcompanion.BaseActivity.getWeatherbyCurrent(BaseActivity.java:164)
at com.hookedroid.fishingcompanion.BaseActivity.getWeather(BaseActivity.java:138)
at com.hookedroid.fishingcompanion.MainFish.syncWeather(MainFish.java:214)
at com.hookedroid.fishingcompanion.MainFish.run(MainFish.java:206)
at java.lang.Thread.run(Thread.java:1019)

Problematic Code

// Gets the user's current NETWORK/GPS location
// then gets weather for that location
private void getWeatherbyCurrent() throws WeatherException {
    Criteria criteria = new Criteria();
    locationProvider = locManager.getBestProvider(criteria, true);

    // Error is thrown on this method call
    getLastKnownLocation();

    getLocationBasedWeather();
}

public void getLastKnownLocation() throws WeatherException {
    String provider;
    //Check if NETWORK Provider is enabled.  If enabled, get it's last known location.
    //If location is received, set baseLocation and return back to get weather.
    //If location is not received, continue on and try to get location from GPS Provider.
    if (isNetworkProviderEnabled()) {
        provider = LocationManager.NETWORK_PROVIDER;
        baseLocation = locManager.getLastKnownLocation(provider);
        // This is Line 337.  I had removed it from the posted code as it was unused
        // and didn't think it was causing an issue.  
        long fixTime = baseLocation.getTime();

        //Location returned from NETWORK_PROVIDER, return/stop executing code and get weather for this location
        if (baseLocation != null)
            return;
    }
    if (baseLocation == null && isGPSEnabled()) {
        provider = LocationManager.GPS_PROVIDER;
        baseLocation = locManager.getLastKnownLocation(provider);
    }
    else {
        if (locationProvider == null) {
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            locationProvider = locManager.getBestProvider(criteria, true);
        }

        if (locationProvider != null)
            baseLocation = locManager.getLastKnownLocation(locationProvider);
        else {
            if (!isGPSEnabled && !isNetworkProviderEnabled)
                throw new WeatherException("No Location Providers are available.");
        }
    }
}
//Returns whether or not the NETWORK_PROVIDER is enabled
public boolean isNetworkProviderEnabled() {
    checkNetworkProvider();
    return this.isNetworkProviderEnabled;
}

//Check if the NETWORK_PROVIDER is enabled
public void checkNetworkProvider() {
    isNetworkProviderEnabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
//Returns whether or not the GPS_PROVIDER is enabled
public boolean isGPSEnabled() {
    checkGPS();
    return this.isGPSEnabled;
}
//Check if the GPS_PROVIDER is enabled
public void checkGPS() {
    isGPSEnabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
hooked82
  • 6,336
  • 4
  • 41
  • 46
  • 1
    Could you perhaps put some comments with the important line numbers? – Cephron Aug 08 '11 at 16:11
  • 2
    Which is the line number 337? – PravinCG Aug 08 '11 at 16:34
  • Pravin: Line 337 is actually missing from the code above, I had removed it as it looked like an unused piece of code that I had forgotten to remove prior to launching. However, line 337 is not re-inserted into the "Problematic Code" section above. – hooked82 Aug 08 '11 at 17:00

3 Answers3

0

Without too much analysis on your code, could it be a permissions issue? Maybe your app isn't allowed to use GPS data, which is causing an unexpected error?

-Woody

Woody
  • 363
  • 2
  • 14
  • I've got all of the correct permissions set and this is the only time I've seen this error reported by hundreds of users – hooked82 Aug 08 '11 at 16:31
  • 1
    As PravinCG mentioned above, can you point out which line is 337? – Woody Aug 08 '11 at 16:39
0

I didnt see where you register for location updates? Maybe that is what you are missing here.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
0

I had accidentally removed a line from my code prior to posting it as I had thought it was just a line of unused code. It was however, causing the problem. In the following line, baseLocation would be equals to null if the NETWORK_PROVIDER is disabled by the user.

long fixTime = baseLocation.getTime();

The fix: Don't try to call the method getTime() on a Null location object :) Thanks everybody!

hooked82
  • 6,336
  • 4
  • 41
  • 46