1

Problem statement
Somehow I cannot retrieve the value of arbetstid from API's "link http://api.arbetsformedlingen.se/af/v0/platsannonser/6309046" from this code below. I retrieve an error message saying

"{"Object reference not set to an instance of an object."}".

However, the arbetstid from API contains data. I don't know what part I'm missing in order to retrieve the data.

Other information

  • I strongly recommend you to review content of the API link and then make a comparison of the data from the class.

  • It works perfectly to retrieve value of "annonsid".

  • I cannot change the XML structure from a API because it is created from a public sector. It means that I have to adapt the way they have created it.

  • The link "What is a NullReferenceException and how do I fix it?" does not provide a solution to my problem statement.

  • The sourcecode has been used and still working based on another context. The different is the API's URL link and XML structure. Link1 Link2

Thank you!
enter image description here

    private static List<annons> myListAnnons = new List<annons>();        
    private static annons _myAnnons = new annons();

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    XDocument xml;

    using (WebClient client = new WebClient())
    {
        client.Headers.Add("Accept-Language", " en-US");
        client.Headers.Add("Accept", "application/xml");
        client.Headers.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");

        xml = XDocument.Parse(client.DownloadString("http://api.arbetsformedlingen.se/af/v0/platsannonser/6309046"));
    }

    xml.Root.Descendants().ToList().ForEach(li =>
    {
        annons _myAnnons = new annons();
        _myAnnons.annonsid = li.Element("annonsid").Value;
        _myAnnons.annonsrubrik = li.Element("annonsrubrik").Value;
        _myAnnons.annonstext = li.Element("annonstext").Value;
        _myAnnons.antal_platser = li.Element("antal_platser").Value;
        _myAnnons.antalplatserVisa = li.Element("antalplatserVisa").Value;
        _myAnnons.arbetstid = li.Element("arbetstid").Value;
        _myAnnons.arbetstidvaraktighet = li.Element("arbetstidvaraktighet").Value;
        _myAnnons.besoksadress = li.Element("besoksadress").Value;
        _myAnnons.egenbil = li.Element("egenbil").Value;
        _myAnnons.epostadress = li.Element("epostadress").Value;
        _myAnnons.hemsida = li.Element("hemsida").Value;
        _myAnnons.kommunnamn = li.Element("kommunnamn").Value;
        _myAnnons.land = li.Element("land").Value;
        _myAnnons.logotypurl = li.Element("logotypurl").Value;
        _myAnnons.loneform = li.Element("loneform").Value;
        _myAnnons.lonetyp = li.Element("lonetyp").Value;
        _myAnnons.namn = li.Element("namn").Value;
        _myAnnons.postadress = li.Element("postadress").Value;
        _myAnnons.postland = li.Element("postland").Value;
        _myAnnons.postnummer = li.Element("postnummer").Value;
        _myAnnons.postort = li.Element("postort").Value;
        _myAnnons.publiceraddatum = li.Element("publiceraddatum").Value;
        _myAnnons.referens = li.Element("referens").Value;
        _myAnnons.sista_ansokningsdag = li.Element("sista_ansokningsdag").Value;
        _myAnnons.telefonnummer = li.Element("telefonnummer").Value;
        _myAnnons.titel = li.Element("titel").Value;
        _myAnnons.varaktighet = li.Element("varaktighet").Value;
        _myAnnons.webbplats = li.Element("webbplats").Value;
        _myAnnons.yrkesbenamning = li.Element("yrkesbenamning").Value;
        _myAnnons.yrkesid = li.Element("yrkesid").Value;

        myListAnnons.Add(_myAnnons);
    });
}
}

public class annons
{
    public String annonsid;
    public String annonsrubrik;
    public String annonstext;
    public String yrkesbenamning;
    public String yrkesid;
    public String publiceraddatum;
    public String antal_platser;
    public String kommunnamn;
    public String antalplatserVisa;
    public String varaktighet;
    public String arbetstid;
    public String arbetstidvaraktighet;
    public String lonetyp;
    public String loneform;
    public String referens;
    public String webbplats;
    public String epostadress;
    public String sista_ansokningsdag;
    public String postnummer;
    public String postadress;
    public String postort;
    public String postland;
    public String land;
    public String besoksadress;
    public String logotypurl;
    public String hemsida;
    public String namn;
    public String titel;
    public String telefonnummer;
    public String egenbil;
} 
Community
  • 1
  • 1
KLN
  • 413
  • 3
  • 8
  • Daniel - have you read my message clearly? The data of "annonsid" exist but it doesn't display it in the datamember. Please remove this mark as a duplicate and read my message again. I have read the content of the "What is a NullReferenceException and how do I fix it?" and it doesn't solve my case. – KLN Jun 07 '15 at 13:01
  • How about writing a self contained example that only retrieves `arbetstid`. Maybe you will figure out the reason at the same time. Looking at the code, you are actually retrieving nothing, because of the xml document structure. – john Jun 07 '15 at 13:08
  • I also have considered about the xml document structure but the question is how to write based on the structure "platsannons > annons > arbetsplats > arbetsplats" in C#? – KLN Jun 07 '15 at 13:29

0 Answers0