Bug 31493 - IFusedLocationProviderApi.RequestLocationUpdates fails with java.lang.NullPointerException
Summary: IFusedLocationProviderApi.RequestLocationUpdates fails with java.lang.NullPoi...
Status: RESOLVED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 5.2
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2015-06-30 04:18 UTC by Kasper
Modified: 2015-07-29 15:21 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Kasper 2015-06-30 04:18:58 UTC
After updating to Xamarin.GooglePlayServices version 25.0.0.0 the following code throws a java.lang.NullPointerException:

	class LocationService : Java.Lang.Object, IGoogleApiClientConnectionCallbacks, IGoogleApiClientOnConnectionFailedListener, global::Android.Gms.Location.ILocationListener 
	{
		private IGoogleApiClient _client;
		private IFusedLocationProviderApi _service;

		public TimeSpan MinTimeSpan { get; set; } = TimeSpan.FromSeconds(10);

		public LocationService ()
		{
			_service = LocationServices.FusedLocationApi;

			_client = new GoogleApiClientBuilder (Forms.Context)
				.AddConnectionCallbacks (this)
				.AddOnConnectionFailedListener(this)
				.AddApi (LocationServices.Api)
				.Build ();

			_client.Connect ();
		}

		public void OnConnected (Bundle connectionHint)
		{
			var request = new LocationRequest ();
			request.SetInterval((int)MinTimeSpan.TotalMilliseconds);
			request.SetPriority (LocationRequest.PriorityBalancedPowerAccuracy);

			_service.RequestLocationUpdates (_client, request, this); // <-- this throws!!!
 		}

		public void OnConnectionSuspended (int cause)
		{
			_service.RemoveLocationUpdates (_client, this);
		}

		public void OnConnectionFailed (ConnectionResult result)
		{
			if (result.HasResolution)
				result.StartResolutionForResult (Forms.Context as Activity, 1001); // why 1001?!
		}

		public void OnLocationChanged (Location location)
		{
			// do something..
		}
	}

I'm running:

Xamarin Studio
Version 5.9.4 (build 5)
Installation UUID: 32815239-f9d8-4d2d-a24b-0f4c02244210
Runtime:
	Mono 4.0.2 ((detached/198235d)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400020005

Xamarin.Android
Version: 5.1.4.8 (Business Edition)
Android SDK: /Users/kasper/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.3    (API level 10)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)
Comment 1 Sunil Kumar 2015-07-07 08:35:50 UTC
I have tried to reproduce this issue with the help of given code but not able to reproduce it.

@Kesper, could you please provide us sample/steps to reproduce this issue at our end.

Also if possible, could you please provide us ide logs.
For XS ide log, location: XS-> Help-> Open Log Directory-> ide.log

Thanks!
Comment 2 Jonathan Pryor 2015-07-08 14:25:38 UTC
Google Play Service's FusedLocationProviderApi.requestLocationUpdates() is throwing a NulPointerException. Search for that:

https://www.google.com/search?q=FusedLocationProviderApi.requestLocationUpdates+NullPointerException

Top hit is stackoverflow:

http://stackoverflow.com/questions/17889126/locationmanager-requestlocationupdates-results-in-nullpointerexception

Which suggests that you need to call GetSystemService(Context.LocationService) *first*:

http://stackoverflow.com/a/17889264/83444
Comment 3 Kasper 2015-07-08 17:44:58 UTC
I am setting the _service member in the ctor with:

           _service = LocationServices.FusedLocationApi;

Notice I'm using IFusedLocationProviderApi not the LocationManager.

If you are implying that the _service member itself is null then you are mistaken.

The sample code I gave works fine with Xamarin.GPS 22.0.0.2.
Comment 4 Jonathan Pryor 2015-07-08 20:04:25 UTC
> If you are implying that the _service member itself is null then you are
> mistaken.

I was not. I was suggesting that com.google.android.gms.location required that you call GetSystemService() in order to work.

On rereading the link, I don't think that's correct, as it doesn't actually deal with Google Play Services.

Sorry.

That said, what is the *complete* stack trace that you get when you run your app? Include the Java-side stack trace. (If an unhandled exception, please provide the relevant `adb logcat` output; if you're `catch`ing the exception, please use `Exception.ToString()`.)
Comment 5 Jonathan Pryor 2015-07-14 15:32:34 UTC
What is the *complete* stack trace that you get when you run your app?
Comment 6 Preetham 2015-07-15 15:44:26 UTC
I am running into the same issue. The (sanitized) stack trace is:

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
[MonoDroid] at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x00078>
[MonoDroid] at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue*) [0x00064] in /Users/builder/data/lanes/1978/f6f57295/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:195
[MonoDroid] at Android.Gms.Location.IFusedLocationProviderApiInvoker.GetLastLocation (Android.Gms.Common.Apis.IGoogleApiClient) <IL 0x00054, 0x00207>
[MonoDroid] at xF.Location.Android.GoogleLocationProvider.OnConnected (Android.OS.Bundle) [0x0003d] in /******/GoogleLocationProvider.cs:173
[MonoDroid] at Android.Gms.Common.Apis.IGoogleApiClientConnectionCallbacksInvoker.n_OnConnected_Landroid_os_Bundle_ (intptr,intptr,intptr) <IL 0x00013, 0x000f3>
[MonoDroid] at (wrapper dynamic-method) object.a6bcdae8-6d0f-4956-9961-d5947fc3141b (intptr,intptr,intptr) <IL 0x00017, 0x00043>
[MonoDroid]   --- End of managed exception stack trace ---
[MonoDroid] java.lang.NullPointerException: Appropriate Api was not requested.
[MonoDroid] 	at com.google.android.gms.common.internal.zzu.zzb(Unknown Source)
[MonoDroid] 	at com.google.android.gms.common.api.zzg.zza(Unknown Source)
[MonoDroid] 	at com.google.android.gms.location.LocationServices.zze(Unknown Source)
[MonoDroid] 	at com.google.android.gms.location.internal.zzd.getLastLocation(Unknown Source)
[MonoDroid] 	at md5942efc827aba1daf9e4888a60fb5dd32.GoogleLocationProvider.n_onConnected(Native Method)
[MonoDroid] 	at md5942efc827aba1daf9e4888a60fb5dd32.GoogleLocationProvider.onConnected(GoogleLocationProvider.java:42)
[MonoDroid] 	at com.google.android.gms.common.internal.zzj.zzg(Unknown Source)
[MonoDroid] 	at com.google.android.gms.common.api.zze.zzmH(Unknown Source)
[MonoDroid] 	at com.google.android.gms.common.api.zze.onConnected(Unknown Source)
[MonoDroid] 	at com.google.android.gms.common.api.zzg$2.onConnected(Unknown Source)
[MonoDroid] 	at com.google.android.gms.common.internal.zzi$zzg.zznO(Unknown Source)
[MonoDroid] 	at com.google.android.gms.common.internal.zzi$zza.zzc(Unknown Source)
[MonoDroid] 	at com.google.android.gms.common.internal.zzi$zza.zzr(Unknown Source)
[MonoDroid] 	at com.google.android.gms.common.internal.zzi$zzc.zznQ(Unknown Source)
[MonoDroid] 	at com.google.android.gms.common.internal.zzi$zzb.handleMessage(Unknown Source)
[MonoDroid] 	at android.os.Handler.dispatchMessage(Handler.java:102)
[MonoDroid] 	at android.os.Looper.loop(Looper.java:135)
[MonoDroid] 	at android.app.ActivityThread.main(ActivityThread.java:5254)
[MonoDroid] 	at java.lang.reflect.Method.invoke(Native Method)
[MonoDroid] 	at java.lang.reflect.Method.invoke(Method.java:372)
[MonoDroid] 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
[MonoDroid] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
[BluetoothLeScanner] onClientRegistered() - status=0 clientIf=5
[AndroidRuntime] Shutting down VM
[System.err] java.lang.NullPointerException: Appropriate Api was not requested.
[System.err] 	at com.google.android.gms.common.internal.zzu.zzb(Unknown Source)
[System.err] 	at com.google.android.gms.common.api.zzg.zza(Unknown Source)
[System.err] 	at com.google.android.gms.location.LocationServices.zze(Unknown Source)
[System.err] 	at com.google.android.gms.location.internal.zzd.getLastLocation(Unknown Source)
[System.err] 	at md5942efc827aba1daf9e4888a60fb5dd32.GoogleLocationProvider.n_onConnected(Native Method)
[System.err] 	at md5942efc827aba1daf9e4888a60fb5dd32.GoogleLocationProvider.onConnected(GoogleLocationProvider.java:42)
[System.err] 	at com.google.android.gms.common.internal.zzj.zzg(Unknown Source)
[System.err] 	at com.google.android.gms.common.api.zze.zzmH(Unknown Source)
[System.err] 	at com.google.android.gms.common.api.zze.onConnected(Unknown Source)
[System.err] 	at com.google.android.gms.common.api.zzg$2.onConnected(Unknown Source)
[System.err] 	at com.google.android.gms.common.internal.zzi$zzg.zznO(Unknown Source)
[System.err] 	at com.google.android.gms.common.internal.zzi$zza.zzc(Unknown Source)
[System.err] 	at com.google.android.gms.common.internal.zzi$zza.zzr(Unknown Source)
[System.err] 	at com.google.android.gms.common.internal.zzi$zzc.zznQ(Unknown Source)
[System.err] 	at com.google.android.gms.common.internal.zzi$zzb.handleMessage(Unknown Source)
[System.err] 	at android.os.Handler.dispatchMessage(Handler.java:102)
[System.err] 	at android.os.Looper.loop(Looper.java:135)
[System.err] 	at android.app.ActivityThread.main(ActivityThread.java:5254)
[System.err] 	at java.lang.reflect.Method.invoke(Native Method)
[System.err] 	at java.lang.reflect.Method.invoke(Method.java:372)
[System.err] 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
[System.err] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
[FlurryAgent] Error logged: uncaught
[FlurryAgent] Trying to end session
[FlurryAgent] Ending session
Resolved pending breakpoint at '/*******/GoogleLocationProvider.cs:173,1' to void xF.Location.Android.GoogleLocationProvider.OnConnected (Android.OS.Bundle p0) [0x00032].
[AndroidRuntime] FATAL EXCEPTION: main
[AndroidRuntime] Process: *****, PID: 28327
[AndroidRuntime] java.lang.NullPointerException: Appropriate Api was not requested.
[AndroidRuntime] 	at com.google.android.gms.common.internal.zzu.zzb(Unknown Source)
[AndroidRuntime] 	at com.google.android.gms.common.api.zzg.zza(Unknown Source)
[AndroidRuntime] 	at com.google.android.gms.location.LocationServices.zze(Unknown Source)
[AndroidRuntime] 	at com.google.android.gms.location.internal.zzd.getLastLocation(Unknown Source)
[AndroidRuntime] 	at md5942efc827aba1daf9e4888a60fb5dd32.GoogleLocationProvider.n_onConnected(Native Method)
[AndroidRuntime] 	at md5942efc827aba1daf9e4888a60fb5dd32.GoogleLocationProvider.onConnected(GoogleLocationProvider.java:42)
[AndroidRuntime] 	at com.google.android.gms.common.internal.zzj.zzg(Unknown Source)
[AndroidRuntime] 	at com.google.android.gms.common.api.zze.zzmH(Unknown Source)
[AndroidRuntime] 	at com.google.android.gms.common.api.zze.onConnected(Unknown Source)
[AndroidRuntime] 	at com.google.android.gms.common.api.zzg$2.onConnected(Unknown Source)
[AndroidRuntime] 	at com.google.android.gms.common.internal.zzi$zzg.zznO(Unknown Source)
[AndroidRuntime] 	at com.google.android.gms.common.internal.zzi$zza.zzc(Unknown Source)
[AndroidRuntime] 	at com.google.android.gms.common.internal.zzi$zza.zzr(Unknown Source)
[AndroidRuntime] 	at com.google.android.gms.common.internal.zzi$zzc.zznQ(Unknown Source)
[AndroidRuntime] 	at com.google.android.gms.common.internal.zzi$zzb.handleMessage(Unknown Source)
[AndroidRuntime] 	at android.os.Handler.dispatchMessage(Handler.java:102)
[AndroidRuntime] 	at android.os.Looper.loop(Looper.java:135)
[AndroidRuntime] 	at android.app.ActivityThread.main(ActivityThread.java:5254)
[AndroidRuntime] 	at java.lang.reflect.Method.invoke(Native Method)
[AndroidRuntime] 	at java.lang.reflect.Method.invoke(Method.java:372)
[AndroidRuntime] 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
[AndroidRuntime] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Thread started: <Thread Pool> #9
[Mono] Assembly Ref addref AndroidApp[0xb4acf3a0] -> System.Core[0x9baee5e0]: 8
[Process] Sending signal. PID: 28327 SIG: 9
Comment 7 Jonathan Pryor 2015-07-15 15:53:59 UTC
Thank you.

The important part is this:

> [MonoDroid] java.lang.NullPointerException: Appropriate Api was not requested.
> [MonoDroid]     at com.google.android.gms.common.internal.zzu.zzb(Unknown Source)

That's Java code throwing the NullPointerException -- from the obfuscated com.google.android.gms.common.internal.zzu.zzb() method -- and the message it provides is "Appropriate Api was not requested."

This sounds like there is some additional configuration that must be performed in order to appease Google Maps, though I don't know what that would be.

So...punt!

https://www.google.com/search?q=com.google.android.gms+NullPointerException+Appropriate+Api+was+not+requested

This looks promising;

http://stackoverflow.com/questions/29190072/android-activityrecognition-java-lang-nullpointerexception-appropriate-api-was
http://stackoverflow.com/a/29196883

> The problem ended up that I was adding the LocationServices.API
> to the Google API Client, but I wasn't adding ActivityRecognition.API.
> After I updated my call to this, it worked:

protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .addApi(ActivityRecognition.API)
                .build();
}
Comment 8 Preetham 2015-07-15 15:55:13 UTC
Further research has led me to the root cause of the problem. Its because of
the use of LocationServices.Api which is deprecated. If that is replaced with
LocationServices.API, then the exception does not occur. I guess the
LocationServices.Api should be marked obsolete instead of deprecated?
Comment 9 Kasper 2015-07-29 05:24:37 UTC
Hi

Sorry for the long delay - I was on vacation.

The stack trace you (Jonathan) requested is here:

at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/1978/f98871a9/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
at Android.Runtime.JNIEnv.CallObjectMethod (intptr,intptr,Android.Runtime.JValue*) [0x00064] in /Users/builder/data/lanes/1978/f98871a9/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:195
at Android.Gms.Location.IFusedLocationProviderApiInvoker.RequestLocationUpdates (Android.Gms.Common.Apis.IGoogleApiClient,Android.Gms.Location.LocationRequest,Android.Gms.Location.ILocationListener) <IL 0x0007c, 0x0031f>
at XamarinPoC.LocationService.OnConnected (Android.OS.Bundle) [0x00035] in /Users/kasper/Projects/xamarinpoc/XamarinPoC/Android/LocationService.cs:55
at Android.Gms.Common.Apis.IGoogleApiClientConnectionCallbacksInvoker.n_OnConnected_Landroid_os_Bundle_ (intptr,intptr,intptr) <IL 0x00013, 0x000f3>
at (wrapper dynamic-method) object.83f86202-273c-47db-bc89-4def079621cb (intptr,intptr,intptr) <IL 0x00017, 0x00043>

  --- End of managed exception stack trace ---
java.lang.NullPointerException: Appropriate Api was not requested.
	at com.google.android.gms.common.internal.zzu.zzb(Unknown Source)
	at com.google.android.gms.common.api.zzg.zza(Unknown Source)
	at com.google.android.gms.common.api.zzd.zza(Unknown Source)
	at com.google.android.gms.common.api.zzd.zzb(Unknown Source)
	at com.google.android.gms.common.api.zzg.zzb(Unknown Source)
	at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source)
	at md5787dbd3d2aae4a1cf4a60ac9e184d171.LocationService.n_onConnected(Native Method)
	at md5787dbd3d2aae4a1cf4a60ac9e184d171.LocationService.onConnected(LocationService.java:34)
	at com.google.android.gms.common.internal.zzj.zzg(Unknown Source)
	at com.google.android.gms.common.api.zze.zzmH(Unknown Source)
	at com.google.android.gms.common.api.zze.onConnected(Unknown Source)
	at com.google.android.gms.common.api.zzg$2.onConnected(Unknown Source)
	at com.google.android.gms.common.internal.zzi$zzg.zznO(Unknown Source)
	at com.google.android.gms.common.internal.zzi$zza.zzc(Unknown Source)
	at com.google.android.gms.common.internal.zzi$zza.zzr(Unknown Source)
	at com.google.android.gms.common.internal.zzi$zzc.zznQ(Unknown Source)
	at com.google.android.gms.common.internal.zzi$zzb.handleMessage(Unknown Source)
	at android.os.Handler.dispatchMessage(Handler.java:102)
	at android.os.Looper.loop(Looper.java:135)
	at android.app.ActivityThread.main(ActivityThread.java:5254)
	at java.lang.reflect.Method.invoke(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:372)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

I can confirm Preethams finding, that switching to LocationServices.API (instead of the obsolete LocationServices.Api) when using the GoogleApiClient.Builder class fixes the issue.
Comment 10 Jonathan Pryor 2015-07-29 15:21:14 UTC
Marking answered as per Comment #8 and Comment #9: use LocationServices.API, NOT LocationServices.Api.