Bug 26716 - NullReferenceException crash on Xamarin.Android only in Release mode when using json.net
Summary: NullReferenceException crash on Xamarin.Android only in Release mode when usi...
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 4.20.0
Hardware: PC Mac OS
: Normal normal
Target Milestone: ---
Assignee: Radek Doulik
URL:
Depends on:
Blocks:
 
Reported: 2015-02-04 04:12 UTC by Prashant Cholachagudda
Modified: 2015-05-07 03:12 UTC (History)
8 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 FIXED

Description Prashant Cholachagudda 2015-02-04 04:12:56 UTC
I'm seeing a NullReferenceException when I try to deserialize some JSON retrieved from an API. Oddly, it only happens for me in a release build on Xamarin.Android (debug builds work fine). I've been able to get things working again by rolling back to v6.0.5.

I put together a Gist with the code to reproduce the issue and the corresponding stack trace it produces. Here is the Json.NET call in question.

var x = "{\"Result\":{\"Location\":\"something\",\"Id\":180478}}";
var y = JsonConvert.DeserializeObject<ApiResult<Something>>(x);

Gist: https://gist.github.com/patridge/38beecbae5908589456b

Originally filed at: https://github.com/JamesNK/Newtonsoft.Json/issues/451
Comment 1 Jonathan Pryor 2015-02-04 09:05:53 UTC
This is probably a linker-related issue: JSON.NET is presumably trying to use Reflection to set fields/properties, and the linker has removed those fields, meaning they don't exist. As such, Type.GetMember("name") will return null, resulting in your NullReferenceException.

What are your linker settings? By any chance, do you have Link All Assemblies enabled?
Comment 2 Paul Read 2015-02-24 09:32:52 UTC
I also had this issue.
Comment 3 Paul Read 2015-02-24 09:33:54 UTC
See fix mentioned by Tim Ricciardi here: https://components.xamarin.com/view/json.net/
Comment 4 peterno 2015-04-28 07:41:10 UTC
This is a major problem for all that uses Newtonsoft.Json on Android and the solution is to follow the 
"See fix mentioned by Tim Ricciardi here:
https://components.xamarin.com/view/json.net/" 
but that is not a solution that should be accepted as it took me two days to find it.

According to https://github.com/JamesNK/Newtonsoft.Json/issues/451 it is not an error on the Newtonsoft codebase, at least not now.

Xamarin has to contact James and find a solution or solve it in the linker somehow.

This is the exception i get:
Exception: System.AggregateException: One or more errors occurred ---> System.NullReferenceException: Object reference not set to an instance of an object
 at System.Linq.Expressions.Expression.CreateLambda (System.Type delegateType, System.Linq.Expressions.Expression body, System.String name, Boolean tailCall, System.Collections.ObjectModel.ReadOnlyCollection 1 parameters) [0x00000] in <filename unknown>:0 
 at System.Linq.Expressions.Expression.Lambda (System.Type delegateType, System.Linq.Expressions.Expression body, System.String name, Boolean tailCall, IEnumerable 1 parameters) [0x00000] in :0 
 
 04-23 14:18:42.817 I/mono-stdout(17797): Exception: System.AggregateException: One or more errors occurred ---> System.NullReferenceException: Object reference not set to an instance of an object
 at System.Linq.Expressions.Expression.Lambda (System.Type delegateType, System.Linq.Expressions.Expression body, System.Linq.Expressions.ParameterExpression[] parameters) [0x00000] in :0 
 at Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory.CreateParametrizedConstructor (System.Reflection.MethodBase method) [0x00000] in :0 
 at Newtonsoft.Json.Serialization.JsonArrayContract.CreateWrapper (System.Object list) [0x00000] in :0 
 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewList (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonArrayContract contract, System.Boolean& createdFromNonDefaultCreator) [0x00000] in :0
Comment 5 Jonathan Pryor 2015-04-29 15:49:35 UTC
@Radek: Sample code: https://gist.github.com/patridge/38beecbae5908589456b
Comment 6 Radek Doulik 2015-05-07 03:12:04 UTC
Works OK on 5.1, assuming it is already fixed.