We are trying to deserialize Bitly data and select some values using linq operators. But it shows System.Nullreference.Exception in anonymous class
Below is our code
static void Main(string[] args)
{
var fileData = File.ReadAllLines("bitlyfile.txt");
var singleRow = fileData.Select(ToJson).ToList();
singleRow.ToList().ForEach(Print);
singleRow
.GroupBy(b => b.Country)
.OrderByDescending(g => g.Count())
.Take(10)
.Select(g => new { CountryName = g.Key, Users = g.Count() })
.ToList()
.ForEach(Print);
Console.ReadKey();
}
public static Bitly ToJson(string json)//
{
return JsonConvert.DeserializeObject<Bitly>(json);
}
public static void Print(object obj)
{
Console.WriteLine(obj);
}
class Bitly
{
[JsonProperty("c")]
public string Country { get; set; }
public override string ToString()
{
return Country;
}
}
Here is our bitly file :Bitly File
It showing an error like below in the line as b is null
.GroupBy(b => b.Country)