- Home /
NullReferenceException: Object reference not set to an instance of an object (XmlDocument.LoadXml())
Hello! I have a problem with XML file when I press Start button on Unity:
NullReferenceException: Object reference not set to an instance of an object AbilitiesExtractor.extraerAbs () (at Assets/Scripts/Abilities/AbilitiesExtractor.cs:22) BattleGUI.Start () (at Assets/Scripts/TurnBasedCombat/BattleGUI.cs:19)
NullReferenceException: Object reference not set to an instance of an object BattleGUI.OnGUI () (at Assets/Scripts/TurnBasedCombat/BattleGUI.cs:30) UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
Here are the classes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.IO;
public class AbilitiesExtractor : MonoBehaviour
{
public TextAsset abilityList;
public static List<Ability> abilities = new List<Ability>();
public List<Ability> extraerAbs()
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(abilityList.text);
XmlNodeList itemList = xmlDocument.GetElementsByTagName("Ability");
Debug.Log("Lista de habilidades: "+itemList.Count);
for (int i = 0; i < itemList.Count; i++)
{
Ability temp = new Ability();
temp.RequiredWeapon = itemList[i].SelectSingleNode("RequiredWeapon").InnerText;
temp.RequiredSkill = itemList[i].SelectSingleNode("RequiredSkill").InnerText;
temp.Range = int.Parse(itemList[i].SelectSingleNode("Range").InnerText);
temp.Name = itemList[i].SelectSingleNode("Name").InnerText;
temp.Description = itemList[i].SelectSingleNode("Description").InnerText;
temp.Target = int.Parse(itemList[i].SelectSingleNode("Target").InnerText);
temp.Power = int.Parse(itemList[i].SelectSingleNode("Power").InnerText);
abilities.Add(temp);
}
return abilities;
}
}
And the second one:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BattleGUI : MonoBehaviour
{
private string playerName;
private int playerLevel;
private int playerTenacity;
private List<Ability> abilities;
AbilitiesExtractor absExtractor = new AbilitiesExtractor();
void Start()
{
playerName = GameInformation.PlayerName;
playerLevel = GameInformation.PlayerLevel;
playerTenacity = GameInformation.Tenacity;
abilities = absExtractor.extraerAbs();
}
void Update()
{
}
void OnGUI()
{
GUI.Button(new Rect(0, 0, 0, 0), abilities[1].Name);
}
}
I have both of them attached to MainCamera, and the XML attached to AbilitiesExtractor. Here is the XML:
<AbilityList>
<Ability>
<RequiredWeapon>Unarmed</RequiredWeapon>
<RequiredSkill level="0">MartialArts</RequiredSkill>
<Range>1</Range>
<Name>Kick</Name>
<Description>Kick that trash!</Description>
<Target>1</Target>
<Power>12</Power>
</Ability>
<Ability>
<RequiredWeapon>Handgun</RequiredWeapon>
<RequiredSkill level="0">Firearms</RequiredSkill>
<Range>1</Range>
<Name>Shot</Name>
<Description>Shot that trash!</Description>
<Target>1</Target>
<Power>10</Power>
</Ability>
</AbilityList>
It is my first time working with XML on unity, but I have other clases working fine with other XML (objects), and I can't see the failure here. I will be very gratefull for any help you can give to me.
EDIT: If I test it on Awake method on the AbilitiesExtractor class (BattleGui removed), it run correctly. So the problem is calling the class, because class1(caller) can't reach xml attached to class2(called)? I don't know how it works. Here is the Awake method:
void Awake()
{
extraerAbs();
foreach(Ability ab in abilities)
{
Debug.Log(ab.Name + "\n" + ab.Description);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612233247im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How do i load Xml and assign variables to player in C#? 1 Answer
How to type Color32 format into XML? 1 Answer
Displaying xml content with rich text 1 Answer
xml loading in unity andriod 1 Answer