locked
Object reference not set to an instance of an object. RRS feed

  • Question

  • User330911564 posted

    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)

    Friday, March 24, 2017 10:59 AM

Answers

  • User-1838255255 posted

    Hi tandohtakyie,

    According to your description, about how to get the specify elements, I think linq is a good way to do that, for more detail, please try the following code:  

    XML file:
    <?xml version="1.0"?>
    <information>
    <details>
    <id>01</id>
    <empname>qwer</empname>
    <empid>001</empid>
    <dept>Accounts</dept>
    <date>2011-01-15 12:21:25</date>
    </details>
    <details>
    <id>02</id>
    <empname>jdfklgd</empname>
    <empid>002</empid>
    <dept>Finance</dept>
    <date>2011-01-20 10:21:25</date>
    </details>
    </information>
     
    
    protected void Button1_Click(object sender, EventArgs e)
            {
                getnodes("01");
            }
    
            public void getnodes(string id)
            {
                var xmlStr = File.ReadAllText(@"C:\Users\v-xindu\Desktop\Publish.xml");
    
    
                var str = XElement.Parse(xmlStr);
    
                var result = str.Elements("details").
            Where(x => x.Element("id").Value.Equals(id)).ToList();
                
            }

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 27, 2017 6:08 AM

All replies

  • User-1838255255 posted

    Hi tandohtakyie,

    According to your description, about how to get the specify elements, I think linq is a good way to do that, for more detail, please try the following code:  

    XML file:
    <?xml version="1.0"?>
    <information>
    <details>
    <id>01</id>
    <empname>qwer</empname>
    <empid>001</empid>
    <dept>Accounts</dept>
    <date>2011-01-15 12:21:25</date>
    </details>
    <details>
    <id>02</id>
    <empname>jdfklgd</empname>
    <empid>002</empid>
    <dept>Finance</dept>
    <date>2011-01-20 10:21:25</date>
    </details>
    </information>
     
    
    protected void Button1_Click(object sender, EventArgs e)
            {
                getnodes("01");
            }
    
            public void getnodes(string id)
            {
                var xmlStr = File.ReadAllText(@"C:\Users\v-xindu\Desktop\Publish.xml");
    
    
                var str = XElement.Parse(xmlStr);
    
                var result = str.Elements("details").
            Where(x => x.Element("id").Value.Equals(id)).ToList();
                
            }

    Best Regards,

    Eric Du

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 27, 2017 6:08 AM
  • User330911564 posted

    thank you for your answer, it is working but how do i populate the (for example: dept) to a asp.net Label?

    Monday, March 27, 2017 11:43 AM
  • User330911564 posted

    thank you, I've been able to fix it

    Tuesday, March 28, 2017 8:27 AM