0

I successfully deserialize JSON File and I'm getting the results but after the end of results I'm getting 'object not set to instance of an object` error

JSON File:

"radiant_team": {
          "team_name": "EHOME",
          "team_id": 4,
          "team_logo": 52122954231169668,
          "complete": true
        },
        "dire_team": {
          "team_name": "Team Secret",
          "team_id": 1838315,
          "team_logo": 543025270456493033,
          "complete": true

public partial class LiveLeagues
{
    public LiveGames Result { get; set; }
}
public class LiveGames
{
    public List<GameStats> games { get; set; }
    public int status { get; set; }
}
 public class GameStats
        {
            public List<BasePlayer> players { get; set; }
            public RadiantTeam radiant_team { get; set; }
            public DireTeam dire_team { get; set; }
        }

public class DireTeam
    {
        public string team_name { get; set; }
        public int team_id { get; set; }
        public object team_logo { get; set; }
        public bool complete { get; set; }
    }

public class RadiantTeam
    {
        public string team_name { get; set; }
        public int team_id { get; set; }
        public object team_logo { get; set; }
        public bool complete { get; set; }
    }

LiveLeagues.LiveLeagues liveGames = JsonConvert.DeserializeObject<LiveLeagues.LiveLeagues>(response.Content.ReadAsStringAsync().Result);

    foreach (var leagues in liveGames.Result.games)
        {
           MessageBox.Show(leagues.dire_team.team_id.ToString());
           MessageBox.Show(leagues.radiant_team.team_id.ToString());  
        }

I tried to iterate through the JSON and test if the values will show. I tested it on MessageBox.Show and I got the result "EHOME" and "Team Secret" but after that the error comes up 'object not set to instance of an object`

7
  • 2
    Please show the complete json and the value of liveGames.Result.games.Count. I guess there is an 'empty' game at the end of your json Commented Jun 5, 2015 at 7:01
  • response.Content.ReadAsStringAsync().Result that is most certainly wrong. Commented Jun 5, 2015 at 7:05
  • this is the JSON File i uploaded it on pastebin, can't post it on my post its too big pastebin.com/P8EPG41X Commented Jun 5, 2015 at 7:05
  • @JeffMercado how so? i have tried it on getting players name and id's it works just fine. Commented Jun 5, 2015 at 7:07
  • Your JSON File contains two livegames, and only the first one has a 'dire_team' and 'radiant_team' property. So for the second legues object, the dire_team will be null... Commented Jun 5, 2015 at 7:09

1 Answer 1

1

When the json allows for games without the dire_team and/or radiant_team property, you should do null checks to make sure they are there:

foreach (var leagues in liveGames.Result.games){ 
  if(leagues.dire_team != null)
    MessageBox.Show(leagues.dire_team.team_id.ToString());
  else
    MessageBox.Show("no dire team for this game");

  if(leagues.radiant_team != null)      
     MessageBox.Show(leagues.radiant_team.team_id.ToString());  
  else
    MessageBox.Show("no radiant team for this game");
}

Or you could try using default values for these objects in the constructor of GameStats.

public class GameStats
    {
        public List<BasePlayer> players { get; set; }
        public RadiantTeam radiant_team { get; set; }
        public DireTeam dire_team { get; set; }

        public GameStats(){
          dire_team = new DireTeam();
          radiant_team = new RadiantTeam();
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for this so ill just null checks every property? is there any non tedious way to do this?
@Nevi: see my edit. But this will not work for every json library. You should test if this works for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.