0

I have a JSON document that I'm trying to deserialize. This is a document that may changed depending on what information is available on certain products. So if there is a product with no downloadable files, there will be missing JSON objects and I need to know if I return null or not. As of now, my application will crash if its null and I don't know how to fix it.

using (WebClient wc = new WebClient())
{
    wc.Headers.Add("User-Agent", "C# Windows Application");
    String jsonData = wc.DownloadString(URL);

    EfobasenRoot EfobasenDeserialized = JsonConvert.DeserializeObject<EfobasenRoot>(jsonData);
                
    // EL Nr
    elNummer = EfobasenDeserialized.Produktskjema.Produktnr;
                
    // Varetekst
    vareTekst = EfobasenDeserialized.Produktinfo.Varetekst;
                
    // Fabrikat
    fabrikat = EfobasenDeserialized.Produktinfo.Fabrikat;

    // FDV Download ID
    // Due to syntax in the Json file, I take the last bbject and save it to a string
    // So we can deserialize it again to get the fileID for the FDV file
    jsonFDV = EfobasenDeserialized.Produktskjema.Skjema.Grupper[2].Felter[0].Verdi.ToString();
    EfobasenFDV EfobasenFDVDeserialized = JsonConvert.DeserializeObject<EfobasenFDV>(jsonFDV);
    fdvNummer = Convert.ToInt32(EfobasenFDVDeserialized.FilId);

    // Download the FDV file from URL
    var saveFile = new SaveFileDialog();
    saveFile.FileName = fabrikat + "-" + vareTekst + "-" + elNummer + "-FDV";
    saveFile.Filter = "PDF document (*.pdf)|*.pdf";

    var result = saveFile.ShowDialog();

    if (result == DialogResult.OK)
    {
        wc.DownloadFile(fdvDownloadPath + fdvNummer, saveFile.FileName);
    }
}

In this code, I need to check "jsonFDV" is empty or not. If it return null now it crashes I don't know how to validate it.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Glenn
  • 1
  • 1
  • `if (jsonFDV != null) { .. //not null } else { // it is null }` – JohnG Dec 12 '21 at 23:51
  • Does not work. Because I'm trying to access an object which does not exists and in turn results in null. – Glenn Dec 12 '21 at 23:52
  • Is the exception being thrown on the line starting with `jsonFDV = EfobasenDeserialized.Pr` or the one starting with `EfobasenFDV EfobasenFDVDeserialized = JsonC` ? – DCAggie Dec 12 '21 at 23:53
  • Yes it is, and this is the error., – Glenn Dec 12 '21 at 23:54
  • System.NullReferenceException: 'Object reference not set to an instance of an object.' Efobasen_AutoFDV.Felter.Verdi.get returned null. – Glenn Dec 12 '21 at 23:54
  • You comment… _”Does not work. Because I'm trying to access an object which does not exists and in turn results in null.”_ … ? … Sorry if I am missing something, however, `jsonFDV` clearly “exists” … otherwise you would not be able to compile the code. So, it is unclear what you mean by… _”an object which does not exists…”_ … ? … – JohnG Dec 13 '21 at 00:13
  • Yes, the reason I get the error is because I'm trying to .get JSON object (Verdi) and save it to jsonFDV, but the problem is thay Verdi does not exist in this current context because itæs not generated by the API I'm using THats my main problem beause I have instances where "Verdi" exists and sometimes not. I juswt have to find a way for the deserializer to validate it or not. – Glenn Dec 13 '21 at 09:16

1 Answers1

0

Use null checking, I'd suggest the Null-Conditional Operator

DCAggie
  • 144
  • 8