I made a level editor with the help of the rather popular “JSON .NET For Unity” free asset from the asset store because Unity’s JsonUtility does not support the objects and collections (object, dictionary, enum, list, queue, tuple) I would like to serialize and deserialize.
Everything was going well in the Unity Editor, but as soon as I tried testing on Android, I got an error presumably due to the deserialization method call as my debug messages before that went through but not the ones after:
Error Unity ArgumentNullException: Value cannot be null.
Error Unity Parameter name: method
Error Unity at Newtonsoft.Json.Utilities.ValidationUtils.ArgumentNotNull (System.Object value, System.String parameterName) [0x00000] in <00000000000000000000000000000000>:0
Error Unity at Newtonsoft.Json.Utilities.LateBoundReflectionDelegateFactory.CreateParameterizedConstructor (System.Reflection.MethodBase method) [0x00000] in <00000000000000000000000000000000>:0
Error Unity at Newtonsoft.Json.Serialization.JsonArrayContract.CreateWrapper (System.Object list) [0x00000] in <00000000000000000000000000000000>:0
Error Unity 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 existingValue, System.String id) [0x00000] in <00000000000000000000000000000000>:0
Error Unity at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue (Newtonsoft.Json.Serialization.JsonProperty pro
I have checked that I’m saving the JSON files into a folder under Resources and that I’m using Resources.Load(“relative/path/under/Resources/to/json/file/without/extension”).text to successfully get the file content.
Is the error likely related to AOT or otherwise related to the lack of updates for “JSON .NET For Unity”? Also, does anyone know if the jilleJr/Newtonsoft.Json-for-Unity repo would be a good alternative to try, or if there are other serialization formats and tips that may help? Any help would be much appreciated.
I imagine once you fix that, your null-ref above will be gone too. But if it’s still there, the answer is always the same… ALWAYS. It is the single most common error ever.
Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!
Some notes on how to fix a NullReferenceException error in Unity3D
also known as: Unassigned Reference Exception
also known as: Missing Reference Exception
also known as: Object reference not set to an instance of an object
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
I made sure to have saved those serialized level data files to the path under Resources before building. I am only having trouble with the deserialize method call.
I’m sorry, I misunderstood this to think you were doing this at runtime.
Could be… one theory to test would be to find the JSON classname that is choking this up, then see if it goes away if you put a constructor call in somewhere so it doesn’t get stripped.
Just to clarify a bit more, the files are saved to the Resources before building, but I am trying to fetch them and have them deserialized at run-time on device.
Thanks for the suggestion.
Hmm, would you mind elaborating on “putting a constructor call for it to not get stripped”? I keep hearing about it, but can’t seems to find details on it.
If you don’t have code somewhere that actually news up stuff with that constructor anywhere else in code, that method could be stripped in an AOT environment and then you wouldn’t be able to hydrate one of those classes.
Using the constructor would prohibit it from being stripped.
“Stripped” here means “code stripping” if you wanna google some more.
Oh! So you mean the constructor for the class whose instances I am trying to serialize and deserialize? I’ll try to just new an instance of it then. Thanks!
Edit: Unfortunately, the issue still persists after I made new instances of the classes I try to serialize and deserialize and rebuild, so maybe it’s not a code stripping issue. Maybe it’s time for me to try the jilleJr/Newtonsoft.Json-for-Unity repo.
Edit2: the jilleJr/Newtonsoft.Json-for-Unity repo fixed the issue whatever it was. Now, the JSON level data files seem to be deserializing successfully.