question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unity: ArgumentNullException: Argument cannot be null

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Comments:8

github_iconTop GitHub Comments

4reactions
WAMMIcommented, Feb 24, 2022

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.

3reactions
eKoopmanscommented, Nov 6, 2022

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:

the constructor has been long gone removed by IL2CPP’s bytecode stripping

I used the AotHelper solution which solved things for me, specifically AotHelper.EnsureList<int>() (EnsureList applies to HashSets).

Read more comments on GitHub >

github_iconTop Results From Across the Web

ArgumentNullException: argument cannot be null
When I run my game I get this message: "ArgumentNullException: argument cannot be null". Where am I wrong?
Read more >
ArgumentNullException: Value cannot be null. : r/Unity3D
This is exactly it. ArugmentNullException s are thrown whenever an argument passed to a method is null and the method doesn't know what...
Read more >
"ArgumentNullException: Value cannot be null ... - Issue Tracker
These streams have old UI. Error: ArgumentNullException: Value cannot be null. Parameter name: _unity_self UnityEditor.UIElements.Bindings.
Read more >
How to Resolve ArgumentNullException in Unity When ...
I have a Unity problem where I am trying to play sound when addScore() function starts using dingSFX.Play();, but instead I get the...
Read more >
Getting "ArgumentNullException: Value cannot be null" error
it is saying that you don't have an AudioSource component on the object so can you don't check that in the editor and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found