2

I am populating an IEnumerable using a LINQ query. The IEnumerable is then passed into another class and appended as a parameter (to a Telerik report). On this line, I'm getting a System.NullReferenceException. Through debugging I've confirmed that this IEnumerable has a list of values so I'm not sure why this is happening.

Here are some relevant snippets of my code:

Class1.cs

//Note that divisionList is an List of PKs and divisionTotalsList is a list of DivisionTotalsModel
foreach (int division in divisionList)
            {

                //LINQ Query
                IEnumerable<DivisionTotalsModel> divSubset =
                    from divno in divisionTotalsList
                    where divno.DivisionNo == division
                    select divno;


                //Create Report
                if(divSubset.Any())
                    Class2.DoStuff(divSubset);
            }

Class2.cs

public void DoStuff(IEnumerable<DivisionTotalsModel> divisionList){
     using (var divisionTotalsReport = new DivisionReport())
        {
          //There are other parameters here that are working successfully
          //This is the line that is failing
          divisionTotalsReport.ReportParameters["list"].Value = divisionList;
        }
}

Why is the error occurring here?

The ReportParameters are working with other parameters (DateTime and int), I just didn't include them in the code snippet.

I've tried ToList() and successfully received a List<T>, but Telerik requires an IEnumerable<T>.

The issue has to lie with the new parameter because I've been able to successfully create the report previously using my other parameters (DateTime and int). The issue has only started when I've tried to add this new IEnumerable parameter.

I've gone through the debugger and looked at the IEnumerable, it says the base is null but when I go to non public members -> source I can see the items.

Solution

I've figured out the problem, I was attempting to feed the data source as a parameter. I've set it as the data source instead and it works perfectly.

user1287523
  • 967
  • 3
  • 14
  • 31
  • 1
    Perhaps you could add the piece of code that initializes `ReportParameters` ? – Andomar May 13 '13 at 15:07
  • Debug the code and see what is actually `null` on that line of code. It's probably not `divisionList`. Odds are that either `ReportParameters` is `null` or (probably more likely) whatever it's indexer returns is `null`. – Servy May 13 '13 at 15:09
  • Have you tried use `.ToArray` in linq? – Fendy May 13 '13 at 15:12
  • Have you checked that `divisionTotalsReport`, `divisionTotalsReport.ReportParameters`, and `divisionTotalsReport.ReportParameters["list"]` are all valid instantiated objects? – Deanna May 13 '13 at 15:17
  • @Deanna yes, I've been able to successfully create a report with other parameters. The issue therefore must lie with the new parameter. – user1287523 May 13 '13 at 15:19
  • When writing inline code, use the back-tick (`) around the code segments to set them apart from standard text. – saluce May 13 '13 at 15:20
  • @user1287523 Don't guess. *We* have to, since we don't have the ability to actually check, but you can just debug the program and *know* which null reference is being accessed; you don't need to guess. – Servy May 13 '13 at 15:24
  • @Servy I'm debugging it right now and have set a breakpoint before the point of failure. `divisionTotalsReport` is not null. `ReportParameters` are also not null. It might be `ReportParameters["list"]` though.... I'll have to look further. Thanks. – user1287523 May 13 '13 at 15:28
  • Did you add a `ReportParameter` named "list" to the report before setting its value? – Gert Arnold May 13 '13 at 17:59

1 Answers1

2

On this line, not just divisionList can be null, and in fact, in this case, it would be fine.

divisionTotalsReport.ReportParameters["list"].Value = divisionList

As the NullReferenceException exception only normally occurs when trying to dereference an object, the candidates are:

  1. divisionTotalsReport
  2. divisionTotalsReport.ReportParameters
  3. divisionTotalsReport.ReportParameters["list"]

If you've confirmed that divisionTotalsReport is set and that ReportParameters is returning a valid reference, then it is most likely that it just doesn't have a list item.

You don't show what initialises DivisionReport but if it's loaded from a report file, you may need to add the list parameter before you can set it.

Deanna
  • 23,876
  • 7
  • 71
  • 156