- Home /
Question by
DanickCyb · Dec 14, 2015 at 09:25 PM ·
nullreferenceexceptionxmlxmlserializer
Why am I geting null reference exception while reading xml file?
hi, here is my problem: I'm trying to load information of my game map that are saved in a xml file, but while reading it, it gives me a null reference exception..
here is the code i'm using:
using UnityEngine;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public static class XMLUtility
{
static string path = "C:/Users/Dan/Documents/projet beta/Assets/map-xml.xml";
public static void LoadXML() {
var xmlSerializer = new XmlSerializer(typeof(itemsDirectory ));
var stream = File.Open(path, FileMode.Open);
Debug.Log(stream);
var deserializedtile = xmlSerializer.Deserialize(stream) as itemsDirectory ;
stream.Close();
for(int i = 0; i < deserializedtile.tile.Length; i++) {
Tiles tile_ = deserializedtile.tile[i];
//Do whatever you want with the data.
Debug.Log(i + tile_.tile.ID);
}
}
}
I'm getting null reference exception on this line : Debug.Log(i + tile_.tile.ID); ObjectReference Not set to an instance of object
and here is my class holding the xml format information :
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
[System.Serializable]
public class Tiles
{
public class Tile
{
[XmlAttribute("posx")]
public int posx = 0;
[XmlAttribute("posy")]
public int posy = 0;
[XmlAttribute("posz")]
public int posz = 0;
[XmlAttribute("house")]
public bool house = false;
[XmlAttribute("isPZ")]
public bool isPZ = false;
[XmlAttribute("ID")]
public int ID = 0;
public class items
{
public class item
{
[XmlAttribute("ID")]
public int item_ID = 0;
}
}
}
[XmlElement("tile")]
public Tile tile;
}
Thx for helping me out!
Comment