Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by SomeGuy22 · Jan 19, 2016 at 06:55 AM · nullreferenceexceptionserializationclass

Null Reference When Setting a Class?

Hi, I wrote a script to make a new CurrentFileData class and set it to the fileSerializer instance, but I'm getting a null reference on the line where I set saveData to newFileData. I tried making instances and even a "setter" function inside the Serializer class, but the SaveShipFile still returns errors.

 function SaveShipFile(v : ShipController)
 {
     var newFileData : CurrentFileData = new CurrentFileData();
     newFileData.ships = new List.<ShipController>();
     var j : ShipController = v;
         //sometimes this line gives an error
     newFileData.ships.Add(j);
     
         //this is the offending line
     fileSerializer.saveData = newFileData;
     fileSerializer.Save(Path.Combine(mdPath + "/Ships", "ShipTest.xml"));
 }
 
 
 @XmlRoot("SavedData")
 public class CurrentFileData {
     
     @XmlArray("Ships")
     @XmlArrayItem("Ship")
     public var ships : List.<ShipController> = new List.<ShipController>();
     
     
 }
 
 
 public class Serializer {
     
     public var saveData : CurrentFileData;
     
     public function Save(path : String)
      {
          var serializer : XmlSerializer = new XmlSerializer(CurrentFileData);
          var stream : Stream = new FileStream(path, FileMode.Create);
          serializer.Serialize(stream, saveData);
          stream.Close();
      }
  
      public static function Load(path : String):CurrentFileData
      {
          var serializer : XmlSerializer = new XmlSerializer(CurrentFileData);
          var stream : Stream = new FileStream(path, FileMode.Open);
          var result : CurrentFileData = serializer.Deserialize(stream) as CurrentFileData;
          stream.Close();
          return result;
      }
 
          public static function LoadFromText(text : String):CurrentFileData
          {
          var serializer : XmlSerializer = new XmlSerializer(CurrentFileData);
          return serializer.Deserialize(new StringReader(text)) as CurrentFileData;
      }
  }

Thanks, any ideas would be appreciated!

Comment
Add comment · Show 1
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 daleth90 · Jan 19, 2016 at 07:12 AM 1
Share

Do you mean Line 10?

If yes, do you have something like fileSerializer = new Serializer() in your code?

1 Reply

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

Answer by SomeGuy22 · Jan 19, 2016 at 05:25 PM

@daleth90 was correct. I missed something so simple as not initializing fileSerializer...

However, it gave another error once that was cleared up. It said it could not implement GameObject.Transform as a serializable attribute, yet none of my classes derived from Monobehaviour. Turns out, just having a List of MonoBehaviours (ShipController) in CurrentFileData was enough to throw it off.

It'll be more complicated now that I have to extract specific data from a MonoBehaviour, but it works.

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.

Follow this Question

Answers Answers and Comments

34 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

Related Questions

"Null Reference Error" when using a custom class as an array 1 Answer

Inpsector missing variables from a Serializable class? 1 Answer

Serialization - Variables won't change on original construction 1 Answer

How to show the instance of a class's variables in the inspector. 0 Answers

Serialized class, Instances and access 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