Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 tcmwaffles · Nov 13, 2011 at 02:28 PM · xmlresources.load

NullReferenceException when trying to load XML

I can't figure out why this is throwing a NullReferenceException when I attach it to a gameobject.Any Help would be greatly appreciated.

 enter code here
 var available_Units : Array = Array();
 var unitList : Array = Array();
 private var UNIT_DATA : String = "unitData.xml";

 function Start(){
 LoadUnitsToArray();
 }

 function LoadUnitStats(unitID){
     var asset : TextAsset = Resources.Load(UNIT_DATA);
     var xmldoc : XmlDocument = XmlDocument();
     xmldoc.LoadXml(asset.text);    
     var statArray = Array();
     var SEARCH_STRING : String = "/player/unit[@id = '" + unitID + "']";
 
     if (xmldoc != null){
         var unit : XmlNode = xmldoc.SelectSingleNode(SEARCH_STRING);
         var unitAttrib : XmlNodeList = unit.ChildNodes;
         for(var i in unitAttrib){
             statArray.Add(i.FirstChild.InnerText);
         }
     }
     else
     Debug.LogError("No XML data File Found");
     return statArray;
 }

 function LoadUnitsToArray(){
     var asset : TextAsset = Resources.Load(UNIT_DATA);
     var xmldoc : XmlDocument = XmlDocument();
     xmldoc.LoadXml(asset.text);
     var allUnits = xmldoc.GetElementsByTagName("unit");
     if (xmldoc !=null){
         for (var i in allUnits){
             var baseUnitStats : Array = LoadUnitStats(i);
             available_Units.Add(baseUnitStats);
         }
     }

}

Comment
Add comment · Show 6
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 syclamoth · Nov 13, 2011 at 02:30 PM 0
Share

Would probably help if you told us where you were getting the null reference exception. There's lots of code here which is not exactly unity-standard, and I'm really not sure what bits might be causing the problem!

avatar image tcmwaffles · Nov 13, 2011 at 02:39 PM 0
Share

Yeah, that probably would help huh? Its says the exception occurs at the xmldoc.LoadXml(asset.text); line. Also, if it seems like I'm doing this in an awkward manner any suggestions are more then welcome.

avatar image syclamoth · Nov 13, 2011 at 02:43 PM 0
Share

Well, that suggests that the line where you assign 'asset' is returning null! Put some Debug.Logs in there, see if you can track down exactly where it's going wrong. Try using the explicit type version of Resources.Load-

 var asset : TextAsset = Resources.Load(UNIT_DATA, TextAsset);

Also, reading the documentation, I think it explicitly states that extensions are omitted! Which means you should strip the '.xml' from the end there.

avatar image tcmwaffles · Nov 13, 2011 at 02:55 PM 0
Share

Thank you very much, removing the .xml extenstion did the trick. Also, I had to change the for loop in LoadUnitsToArray to for (i = 0; i < allUnits.Count; i++) since it was passing a element not an int.

avatar image syclamoth · Nov 13, 2011 at 02:59 PM 0
Share

Oh yeah, I didn't even see that one!

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Nov 13, 2011 at 05:36 PM

From syclamoth: "strip the ".xml" extension from the end there"

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to check the path on Android 0 Answers

Xml Loading Issue 1 Answer

A node in a childnode? 1 Answer

Convert XML Deserializer from folder to resources 2 Answers

XML Error on Load 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