0

I have the lines

 if(query.GetRecordsFromResults() != null)
 RecordsList.AddRange(query.GetRecordsFromResults());

Where GetRecordsFromResults() returns IList<IRecord>. Despite the if condition, i get System.ArgumentNullException. Why is that ?

The faulty method :

 foreach (var query in QueryList)
 {
    query.ExecutePreparedQuery();
    if(query.GetRecordsFromResults() != null)
    RecordsList.AddRange(query.GetRecordsFromResults());
 }

QueryList is a List<Query>, query is a Query. Both are implemented, instantiated and initialised (verified).

 public override IList<IRecords> GetRecordsFromResults()
 {
   var recordsList = new List<IRecords>();

   if (Result != null && Result.Count != 0)
   {
      RecordBuilder.Value = Result.Count;
      RecordBuilder.Querytype = QueryDescription();
      RecordBuilder.Dateformatted = DateFormatting();
      RecordBuilder.WebTitle = Web.Title;

      recordsList.Add(RecordBuilder.BuildRecord());

      return recordsList;
 }

      RecordBuilder.Value = 0;
      RecordBuilder.Querytype = QueryDescription();
      RecordBuilder.Dateformatted = DateFormatting();
      RecordBuilder.WebTitle = Web.Title;

      recordsList.Add(RecordBuilder.BuildRecord());

      return recordsList;
}

Result is a Query's property, updated during the call of Query.ExecutePreparedQuery. It may be null if the query failed. RecordBuilder is a builder for IRecord implementations, it is a bunch of string and int properties. QueryDescription is a self-describing method of Query returning a string. DateFormating returns a string from a DateTime property.

The environement is not multithreaded, Query is an abstract class, the shown GetRecordFromResults is the override in an abstract child class (below that other child class are real implementations).

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Ythio Csi
  • 379
  • 2
  • 14
  • Is RecordsList a collection you initialized before? – Izuka May 18 '17 at 11:57
  • 2
    Why are you calling the same method two times? then What is happening inside `GetRecordsFromResults()`? what is the type of `RecordsList` – sujith karivelil May 18 '17 at 11:58
  • @Isuka RecordList is a property and initialized in the constructor `RecordsList = new List();` – Ythio Csi May 18 '17 at 11:59
  • @un-lucky The lines shown are called in a foreach loop indeed. `RecordsList` is a `List`. Inside `GetRecordsFromResults()` is instantiated a `List`, filled depending on the two outcomes of a if condition, and returned. – Ythio Csi May 18 '17 at 12:02
  • Maybe your function also should return an empty list, even if the query doesn't return any result. This way, you don't even have to do this test. – Izuka May 18 '17 at 12:02
  • 1
    Could you please test that `RecordsList` isn't null, even if you initialized it in your constructor? – Izuka May 18 '17 at 12:08
  • This is a debugging question and does not provide value to others visiting this question. There are too many unknowns in this question for proper help to be provided. It is only going to draw guesses from those trying to help. – Nkosi May 18 '17 at 12:11
  • @Isuka the exception is `ArgumentNullException` based on OP. if `RecordsList` was null it would be a different null exception. – Nkosi May 18 '17 at 12:13
  • @Nkosi What kind of unknowns do you need to be cleared ? `List(T).AddRange` raises `ArgumentNullException` when passed argument is null. I test for null a line before yet exception is still raised. – Ythio Csi May 18 '17 at 12:16
  • @Isuka I tested if `RecordsList` is null and throw an exception if it is. No exception happened – Ythio Csi May 18 '17 at 12:20
  • @YthioCsi Was my bad, I misreaded and thought it was a NullReferenceException. The problem is probably coming from the definition of the result list you return in your query function. – Izuka May 18 '17 at 12:22
  • @YthioCsi A lot of you responses to comments should have been in the OP to begin with. The fact that everyone has to ask that means that the question was not complete. Here are some other question? What type is `query`? is `GetRecordsFromResults` a member or extension method? Is this being execute in a multi-threaded environment?...the list can continue but there are only so many characters allowed in a comment. you need to provide a [mcve] that can be used to reproduce the problem to better help in providing you with useful answer. not more questions – Nkosi May 18 '17 at 12:23
  • @Isuka I provided much more information based on your questions. Now you should have a minimal, complete verifiable example i believe. – Ythio Csi May 18 '17 at 12:41
  • @Nkosi I edited to provide much more informations – Ythio Csi May 18 '17 at 12:42
  • @YthioCsi while this is an improved question it only leads to more questions. Suggestion: Create a unit test for the `Query` class and see if `GetRecordsFromResults` can be called multiple times and have the same result. – Nkosi May 18 '17 at 12:47

1 Answers1

3

Apart from the Exception that you are getting, the way you are doing is wrong, it would be something like the following;

// Make sure that you have initialized/instantiated RecordsList
var iListResult = query.GetRecordsFromResults();
if(iListResult != null)
{
   RecordsList.AddRange(iListResult);
}

Obviously this will solve your issue. This is because(may be) the things happening inside the GetRecordsFromResults() which means the first call returning the expected result(that's why the condition evaluated to true), and the second call returns a null.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • @YthioCsi: Could you please tell me, the type of `RecordsList` and the details of the exception that you are geting? – sujith karivelil May 18 '17 at 12:09
  • I tried, I get a null reference exception instead. RecordsList is a `List` – Ythio Csi May 18 '17 at 12:13
  • @YthioCsi: Use breakpoints to Make sure that `query` is not null and everything happening fine inside the `GetRecordsFromResults()` – sujith karivelil May 18 '17 at 12:20
  • `query` can't be null since i call one of its method a line before. I'll check inside `GetRecordsFromResults()` but it is a pretty straightfoward method : a new `List` filled in one of two ways depending on a if condition, and a return the list. – Ythio Csi May 18 '17 at 12:23