0

I am trying to join 2 arrays but getting exception with linq inner join:

Object reference not set to an instance of an object.

Code :

 public class model
    {
        public string Id { get; set; }
        public string Value { get; set; }
    }
class Program
    {
        static void Main(string[] args)
        {
            var sourceData = GetData(SourceConnectionstring,
                                    "select Id,Value from source");

            var targetData = GetData(TargetConnectionstring,
                                    "select Id,Value from target");


            var data = (from s in sourceData
                        join t in targetData on s.Id equals t.Id
                        where s.Value != t.Value
                        select new
                        {
                            srcKey = s.Id,
                            srcSnapshot = s.Value,
                            tgtKey = t.Id,
                            tgtSnapshot = t.Value
                        }).ToArray();
        }


        public static model[] GetData(string connectionString, string sqlQuery)
        {
            model[] m = new model[1000];
            using (var sqlConnection = new SqlConnection(connectionString))
            {
                using (var command = new SqlCommand(sqlQuery, sqlConnection))
                {
                    sqlConnection.Open();
                    using (var reader = command.ExecuteReader())
                    {
                        var id = reader.GetOrdinal("Id");
                        var value = reader.GetOrdinal("Value");
                        int c = 0;
                        while (reader.Read())
                        {
                            model n = new model
                            {
                                Id = reader.IsDBNull(id) ? null : reader.GetString(id),
                                Value = reader.IsDBNull(value) ? null : reader.GetString(value)
                            };
                            m[c] = n;
                            c = c + 1;
                        }
                        reader.Close();
                    }
                }
                sqlConnection.Close();
            }
            return m;
        }
    }

Getting Error on t.Id:

equals t.Id

I want to join 2 arrays based on Id and get records whose Value is not matching.

I am not getting whats the problem.

Can anybody please help me with this?

ILoveStackoverflow
  • 1,973
  • 1
  • 11
  • 35
  • 1
    @HimBromBeere But this error is in reference to linq query so how this can be duplicate? – ILoveStackoverflow Feb 01 '18 at 12:44
  • 1
    I think size of collection you predefined to 1000. However values filled by db may be lesser than that. So join will effectively will fail for those unassigned null value in array. make sense ? you try using list and add it to that list. – rahulaga_dev Feb 01 '18 at 12:50
  • 1
    The duplicate covers all null reference exceptions because they are very common and really come down to debugging to figure out what is null and why it's null. – juharr Feb 01 '18 at 12:50
  • @juharr Actually i have blindly taken size of array as 1000 because i dont know how to make array size based on number of records. – ILoveStackoverflow Feb 01 '18 at 12:53
  • @Rahul I too think thats the problem but i want to see generic array performance but i am not getting how to assign array size as per number of database table records hence i have taken it as 1000 however number of records in table are 293 – ILoveStackoverflow Feb 01 '18 at 12:54
  • @User You should use a `List` like Rahul suggests instead of an array. Only use arrays when you know the size before hand and you don't want to add or remove from it. – juharr Feb 01 '18 at 12:54
  • @juharr I dont want to add or remove item from array hence i have taken generic array and moreover generic array are faster than list i guess which i want to test – ILoveStackoverflow Feb 01 '18 at 12:56
  • @User A `List` is just a dynamic array and if you give it an initial size like 1000 then it will internally create an array of that size and will not resize it until you go over that number of items, thus negating any noticiable speed difference. But really you should be more concerned with correct and readable code before worrying about micro-optimizations like this. – juharr Feb 01 '18 at 12:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164339/discussion-between-user-and-juharr). – ILoveStackoverflow Feb 01 '18 at 12:59
  • Obviously either `s` or `t` is `null`. However, as juharr already mentioned, all nullreferenceexceptions are quite similar and can be examined by some debugging. Just let your debugger break on any exception and you´ll see where exactly it is caused. – HimBromBeere Feb 01 '18 at 13:03

0 Answers0