Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top