0

I want to deserialize a JSON that was given to me from a DB

That's my Dto

public class filiali {
       public cliente cli { get; set; }

       public int fil { get; set; }

       public string nom { get; set; }

       public int km { get; set; }

       public decimal ore { get; set; }
   }

   // filiale singola
   public class FilialeDto {
       public filiali filiali { get; set; }
   }

   // lista di filiali
   public class ListaFilialiDto {
       public List<filiali> filiali { get; set; }
   }

My Function

        private static ListaFilialiDto ProcessFiliali() {
            ListaFilialiDto filialeDto = null;

            try {
                var jsonString = client.GetStringAsync("my service").Result;
                filialeDto = JsonConvert.DeserializeObject<ListaFilialiDto>(jsonString);
            } catch (Exception ex) {
                logger.Error(ex, "Errore chiamata Servizio Filiali");
            }

            return filialeDto;
        }

my json

{"filiali":[{"cli":1,"fil":1,"nom":"Tizio SPA","km":0,"ore":0.00}]}

the problem's that my object "filialeDto" is null and I get the following:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Tony Abrams
  • 4,224
  • 3
  • 20
  • 31
CSharper
  • 21
  • 7
  • 1
    Do you get an exception, that you log? Because if you do, the code will continue with `filialeDto` being `null`. – Lasse V. Karlsen Jun 13 '19 at 14:04
  • 3
    However, you have something odd here, your JSON contains `"cli":1`, strongly suggesting that `cli` is a number, and your class contains `public cliente cli { get; set; }` strongly suggesting `cliente` is a class, this will not deserialize properly without custom deserializers, which you have not shown, so I'm assuming you're not using that. Most likely this causes an exception (that you log, but then allow the code to execute further), which results in `filialeDto` not being assigned to. – Lasse V. Karlsen Jun 13 '19 at 14:05
  • Give this a try var result = JsonConvert.DeserializeObject>(jsonString) and return the result and see if its null – nalnpir Jun 13 '19 at 14:05
  • @LasseVågsætherKarlsen Ah yes, that is exactly the problem, I missed that one. – DavidG Jun 13 '19 at 14:07
  • that' s the exception System.NullReferenceException: 'Object reference not set to an instance of an object.' filiali was null. – CSharper Jun 13 '19 at 14:07
  • 3
    No, the null reference exception is from outside of this method because you swallow the real exception with your try/catch. – DavidG Jun 13 '19 at 14:08
  • 1
    Comment out the try/catch there and see what happens, most likely the real problem will pop up immediately. – Lasse V. Karlsen Jun 13 '19 at 14:09
  • The code as posted will not give you NullReferenceException, if you get that then that is the result of code you have not posted, so there is no way we can answer that. Additionally, but again I think this exception is a red herring, if you insist this is the problem, the question will be closed as a duplicate because the solution to that question is to debug, debug, debug, and we have a very good answer handling that duplicate case. – Lasse V. Karlsen Jun 13 '19 at 14:10
  • @nalnpir that will not work as there is a parent level to the list of objects. Yours would work if the JSON looked like `[{"cli":1,"fil":1,"nom":"Tizio SPA","km":0,"ore":0.00}]` – Chris B Jun 13 '19 at 14:11
  • Since you have posted code that doesn't compile (no `cliente` definition), nor will run (we don't have your webservice), I think at this point I must insist that you post a [mcve], as this will likely reveal the root cause of the problem to you as well. The code as posted, inferring that `cliente` is a class, will throw a `JsonSerializationException` with this message: "Error converting value 1 to type 'cliente'. Path 'filiali[0].cli', line 1, position 20." – Lasse V. Karlsen Jun 13 '19 at 14:12
  • Now, if `cliente` is an enum, this will run, and deserialize, and throw no exception (assuming the web service *actually* returns the given JSON example), so again, [mcve]. – Lasse V. Karlsen Jun 13 '19 at 14:13
  • As a general advice, don't use `.Result` on task not yet awaited. `await` the task and grab the value from there. – Tsahi Asher Jun 13 '19 at 14:18
  • cliente was another class where the prop cli's an int, i tried to change the prop cli to an int and it worked thanks – CSharper Jun 13 '19 at 14:21
  • https://dotnetfiddle.net/zKAMwS i did try his code online. cli must be a string or int and if that doesnt solve it, the problem must be in the async part – nalnpir Jun 13 '19 at 14:25

0 Answers0