• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by DerrickLau · Apr 24, 2019 at 03:25 AM · serialization.net

Unity3D UWP app returns null on typeof() call.

I have an Unity 3D 2018.2 build w$$anonymous$$ch I am using to build an UWP game. I have included two assemblies (DLLs) built on .NET standard 2.0 written in C#. The first assembly instantiates a DataContractJsonSerializer object to deserialize JSON messages received from a server into a C# class defined in the second assembly. To the DataContractJsonSerializer() constructor I pass in a typeof(myclass). I run t$$anonymous$$s code in the editor and it works fine, but when I build the UWP app and run that, in my logs I get an exception thrown:

System.NullReferenceException: Object reference not set to an instance of an object. at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.TryWritePrimitive (System.Type type, System.Func`1[TResult] value, System.Reflection.MemberInfo memberInfo, System.Nullable`1[T] arrayItemIndex, System.Xml.XmlDictionaryString name, System.Int32 nameIndex) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.WriteMembers (System.Runtime.Serialization.ClassDataContract classContract, System.Runtime.Serialization.ExtensionDataObject extensionData, System.Runtime.Serialization.ClassDataContract derivedMostClassContract) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.WriteClass (System.Runtime.Serialization.ClassDataContract classContract) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.WriteToJson (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.Runtime.Serialization.ClassDataContract dataContract, System.Xml.XmlDictionaryString[] memberNames) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonFormatWriterGenerator+CriticalHelper+<>c_DisplayClass0_0.b0 (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.Runtime.Serialization.ClassDataContract dataContract, System.Xml.XmlDictionaryString[] memberNames) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonFormatClassWriterDelegate.Invoke (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.Runtime.Serialization.ClassDataContract dataContract, System.Xml.XmlDictionaryString[] memberNames) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonClassDataContract.WriteJsonValueCore (System.Runtime.Serialization.XmlWriterDelegator jsonWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonDataContract.WriteJsonValue (System.Runtime.Serialization.XmlWriterDelegator jsonWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in :0 at System.Runtime.Serialization.Json.DataContractJsonSerializer.WriteJsonValue (System.Runtime.Serialization.Json.JsonDataContract contract, System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in :0 at System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson.WriteDataContractValue (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in :0 at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in :0 at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Boolean isDeclaredType, System.Boolean writeXsiType, System.Int32 declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in :0 at System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.InternalSerialize (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Boolean isDeclaredType, System.Boolean writeXsiType, System.Int32 declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in :0 at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Boolean isDeclaredType, System.Boolean writeXsiType, System.Int32 declaredTypeID, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in :0 at System.Reflection.MonoMethod.InternalInvoke (System.Object obj, System.Object[] parameters, System.Exception& exc) [0x00000] in :0 at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in :0 at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.InternalSerialize (System.Reflection.MethodInfo methodInfo, System.Func`1[TResult] memberValue, System.Type memberType, System.Boolean writeXsiType) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.WriteValue (System.Type memberType, System.Object memberValue) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.WriteMembers (System.Runtime.Serialization.ClassDataContract classContract, System.Runtime.Serialization.ExtensionDataObject extensionData, System.Runtime.Serialization.ClassDataContract derivedMostClassContract) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.WriteClass (System.Runtime.Serialization.ClassDataContract classContract) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonFormatWriterInterpreter.WriteToJson (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.Runtime.Serialization.ClassDataContract dataContract, System.Xml.XmlDictionaryString[] memberNames) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonFormatWriterGenerator+CriticalHelper+<>cDisplayClass0_0.b_0 (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.Runtime.Serialization.ClassDataContract dataContract, System.Xml.XmlDictionaryString[] memberNames) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonFormatClassWriterDelegate.Invoke (System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.Runtime.Serialization.ClassDataContract dataContract, System.Xml.XmlDictionaryString[] memberNames) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonClassDataContract.WriteJsonValueCore (System.Runtime.Serialization.XmlWriterDelegator jsonWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in :0 at System.Runtime.Serialization.Json.JsonDataContract.WriteJsonValue (System.Runtime.Serialization.XmlWriterDelegator jsonWriter, System.Object obj, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in :0 at System.Runtime.Serialization.Json.DataContractJsonSerializer.WriteJsonValue (System.Runtime.Serialization.Json.JsonDataContract contract, System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson context, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in :0 at System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson.WriteDataContractValue (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.RuntimeTypeHandle declaredTypeHandle) [0x00000] in :0 at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.Boolean verifyKnownType, System.RuntimeTypeHandle declaredTypeHandle, System.Type declaredType) [0x00000] in :0 at System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson.SerializeWithXsiTypeAtTopLevel (System.Runtime.Serialization.DataContract dataContract, System.Runtime.Serialization.XmlWriterDelegator xmlWriter, System.Object obj, System.RuntimeTypeHandle originalDeclaredTypeHandle, System.Type graphType) [0x00000] in :0 at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalWriteObjectContent (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph) [0x00000] in :0 at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalWriteObject (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph) [0x00000] in :0 at System.Runtime.Serialization.XmlObjectSerializer.InternalWriteObject (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x00000] in :0 at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) [0x00000] in :0 at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions (System.Runtime.Serialization.XmlWriterDelegator writer, System.Object graph) [0x00000] in :0 at System.Runtime.Serialization.Json.DataContractJsonSerializer.WriteObject (System.Xml.XmlDictionaryWriter writer, System.Object graph) [0x00000] in :0 at System.Runtime.Serialization.Json.DataContractJsonSerializer.WriteObject (System.IO.Stream stream, System.Object graph) [0x00000] in :0

In Unity3D editor both assemblies are set to run on IL2CPP. I set the API compatibility level to .NET standard 2.0.

I tried copying the type into the same assembly as the call to instantiate the DataContractJsonSerializer but still get the same error.

Why is it that when I run it from the editor it works, but the UWP app fails?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DerrickLau · Apr 24, 2019 at 10:44 PM 0
Share

Essentially it dies on this line of code:

             Type messageType = typeof(StupidFun.Unity3D.Messages.Message);
             Debug.Log(@"CommunicationBase.SerializeMessage(): Got type and passing into constructor of DataContractJsonSerializer.");
             DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(messageType);

The thing is the type StupidFun.Unity3D.Messages.Message is in the same assembly as the code above, so why am I getting a NULL pointer in messageType??

avatar image DerrickLau · Apr 24, 2019 at 10:45 PM 0
Share

Also, is this an Unity3D issue or .NET standard issue? Don't really see it as an Unity3D issue since I'm only using .NET standard 2.0 APIs in my code...

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DerrickLau · May 04, 2019 at 02:18 AM

Found the answer from a post a few months earlier: https://issuetracker.unity3d.com/issues/system-dot-configuration-dot-configurationerrorsexception-failed-to-load-configuration-section-for-datacontractserializer

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

174 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Windows Store build errors inside Unity 1 Answer

.Net 3.5 to 4.6 Upgrade --> messed up scales at Collada Models 0 Answers

Loading and Saving error 1 Answer

Resolve AnimationClip curve binding at runtime. 0 Answers

Target field of a MonoBehaviour attribute ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges