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);
}