XML class Error : Object reference not set to an instance of an object in ASP.Net

Last Reply on Apr 04, 2017 05:04 AM By tandoh

Posted on Mar 24, 2017 05:05 AM

I'm try to fetch unique number with the other extra information that belong to the specified unique number but it "Object reference not set to an instance of an object.". I have created this class and when i run, it only fetches the unique and display it well but it does not fetch the other values that belongs to the unique number. I've searched for many examples but i just can't fine anyone that help to fix this.

this is my .cs

 

public Advertentie GetDetails(string unieknummer)
        {
            XmlRootAttribute xRoot = new XmlRootAttribute();
            xRoot.ElementName = "root";
            xRoot.IsNullable = true;

            XmlSerializer deserializer = new XmlSerializer(typeof(Advertentie), xRoot);
            StreamReader reader = new StreamReader("C:\\Users\\Gabriel\\documents\\visual studio 2013\\Projects\\ADadvertisement\\ADadvertisement\\Xml\\advertenties.xml");
            Advertentie rootObj = (Advertentie)deserializer.Deserialize(reader);
            rootObj.Unieknummer = unieknummer;
            return rootObj;
        }

and in my Page_load()

 

 

var obj = new XMLHelper();
            Advertentie adv = obj.GetDetails(Request.QueryString["unieknummer"]);


            lblTopModel.Text = adv.Unieknummer; //(this one works fine)
            lblTopMerk.Text = Convert.ToString(adv.Laatstemutatie);//(this one doesn't show anything)
            lblModel.Text = adv.Auto.Model; //(this is gives me the error Object reference no set to an instance of an object)

 

Posted on Apr 04, 2017 05:04 AM

I've been able to do this one. Root should be root with small letter r.

public List<Advertentie> GetList()
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(root));
            StreamReader reader = new StreamReader("C:\\Users\\Gabriel\\documents\\visual studio 2013\\Projects\\ADadvertisement\\ADadvertisement\\Xml\\advertenties.xml");
            Root rootObj = (root)deserializer.Deserialize(reader);
            return rootObj.Advertentie;
 
        }