Unity: ArgumentNullException: Argument cannot be null
See original GitHub issueI’m having a issue parsing a json message using Unity.
When running the build on the editor (Mac) everything works perfect, but on the Android device, it blows up when trying to parse the QuestionDto list.
I’m I doing something wrong or is this a legit bug?
Thanks in advance!
Source/destination types
public class GameDetails {
public string id;
public SagaMechanic mechanic;
public IList<QuestionDto> questions;
public Dictionary<string, object> definitions;
public GameDetails(string Id, IList<QuestionDto> questions, SagaMechanic mechanic,
Dictionary<string, object> definitions)
{
id = Id;
this.questions = questions;
this.mechanic = mechanic;
this.definitions = definitions;
}
}
public class QuestionDto {
public string id ;
public string gameId ;
public string category ;
public string questionAsked ;
public List<AnswerDto> answers ;
public int correctAnswer ;
public QuestionDto(string id, string gameId, string category, string questionAsked, List<AnswerDto> answers,
int correctAnswer)
{
this.id = id;
this.gameId = gameId;
this.category = category;
this.questionAsked = questionAsked;
this.answers = answers;
this.correctAnswer = correctAnswer;
}
}
public class AnswerDto {
public string text { get; private set; }
public bool enabled { get; private set; }
public AnswerDto(string text, bool enabled = true)
{
this.text = text;
this.enabled = enabled;
}
}
Source/destination JSON
{
"id": "some_game",
"mechanic": 0,
"questions": [
{
"Id": "12",
"Category": 5,
"QuestionAsked": "Some Question?",
"Answers": [
{
"Text": "first",
"Enabled": true
},
{
"Text": "second",
"Enabled": true
},
{
"Text": "third",
"Enabled": true
},
{
"Text": "fourth",
"Enabled": true
}
],
"CorrectAnswer": 1
}
]
}
Expected behavior
Actual behavior
Error path: questions[0].answers
Error message: ArgumentNullException: Argument cannot be null. Parameter name: method at Newtonsoft.Json.Utilities.ValidationUtils.ArgumentNotNull (System.Object value, System.String parameterName) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Utilities.LateBoundReflectionDelegateFactory.CreateParameterizedConstructor (System.Reflection.MethodBase method) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Serialization.JsonArrayContract.CreateWrapper (System.Object list) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewList (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonArrayContract contract, Boolean& createdFromNonDefaultCreator) [0x00000] in <filename unknown>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.Object existingVal
Steps to reproduce
JsonConvert.DeserializeObject<GameDetails>(body.ToString())
Issue Analytics
- State:
- Created 6 years ago
- Comments:8
Sorry for commenting on such an old issue, but just wanted to leave a comment and let you know that we faced the same issue with some swagger-generated code. It used ICollection as generic arrays. Our app did not work on Android (Oculus Quest 2) but did so in the Unity Editor.
For us, changing ICollection to List fixed the issue.
Also sorry to resurrect, but this symptom had a different solution for me. For context, I didn’t have any IList/ICollection/etc, and additional empty constructors weren’t required.
I was getting this error in Unity when compiling to Oculus (Android), specifically when deserializing
HashSet<int>
. Turns out it was caused by the ahead-of-time (AOT) compilation in IL2CPP - as described in one of the solutions:I used the AotHelper solution which solved things for me, specifically
AotHelper.EnsureList<int>()
(EnsureList applies to HashSets).