Bug 2760 - AsyncTask dies with NPE if TReturn is `string` and RunInBackground() returns null
Summary: AsyncTask dies with NPE if TReturn is `string` and RunInBackground() returns ...
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.0
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-01-06 12:39 UTC by Jonathan Pryor
Modified: 2012-04-23 07:03 UTC (History)
2 users (show)

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

Scratch.AsyncTask.zip (9.88 KB, application/zip)
2012-01-06 12:39 UTC, Jonathan Pryor

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:

Description Jonathan Pryor 2012-01-06 12:39:17 UTC
Created attachment 1132 [details]

A class which inherits from AsyncTask<,,string> and overrides RunInBackground() throws a NullPointerException:

	class MyAsyncTask : AsyncTask<string, string, string> {
		protected override string RunInBackground (params string[] @params)
			return null;

> E/mono    (23556): Unhandled Exception: Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
> E/mono    (23556):   at Android.Runtime.JNIEnv.NewObject (IntPtr jclass, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00000] in <filename unknown>:0 
> E/mono    (23556):   at Java.Lang.String..ctor (System.String string) [0x00000] in <filename unknown>:0 
> E/mono    (23556):   at Android.Runtime.JavaTypeConverter`1+Convert[System.String].ToJavaObject (System.String value) [0x00000] in <filename unknown>:0 
> E/mono    (23556):   at Android.Runtime.JavaTypeConverter`1+StringConverter[System.String].ToNative (System.String val) [0x00000] in <filename unknown>:0 
> E/mono    (23556):   at Android.OS.AsyncTask`3[System.String,System.String,System.String].DoInBackground (Java.Lang.Object[] native_parms) [0x00000] in <filename unknown>:0 
> E/mono    (23556):   at Android.OS.AsyncTask.n_DoInBackground_arrayLjava_lang_Object_ (IntPtr jnienv, IntPtr native__this, IntPtr native__params) [0x00000] in <filename unknown>:0 
> E/mono    (23556):   at (wrapper dynamic-method) object:bdab0283-7f57-4562-85c8-c45dfd37bad7 (intptr,intptr,intptr)

Building and running the attached project generates the above exception at runtime.
Comment 1 Atsushi Eno 2012-03-08 10:26:46 UTC
Is this MSBuild task issue?
Comment 2 Jonathan Pryor 2012-03-08 10:40:02 UTC
No, this is a core runtime issue. JavaTypeConverter<T>.Convert.ToJavaObject(string) doesn't check for a null string, and thus effectively does `new Java.Lang.String((string) null)`, which isn't really correct. :-)

We need to fix Convert.ToJavaObject(string) to return null when the value is null instead of creating a wrapper over a null instance. Then we'll need to check the rest of the Convert methods to make sure they also handle null properly.
Comment 3 Atsushi Eno 2012-04-23 07:03:49 UTC
Fixed (along with some other converter methods).