System.InvalidOperationException when one Object in List of Data in JsonDataSource is null

Hi,

I am using the Aspose.Words (.net) LinqReportingEngine with a JsonDataSource to generate documents for multiple persons. In my example, I have a list of multiple persons, and for each person, I want to create a separate page in the generated document.

My person-class looks like this:

 public class Person
{
    public Persondata Persondata { get; set; }
    public Address Address { get; set; }
    public int Age { get; set; }
    public string Email { get; set; }
}

public class Persondata
{
    public string FirstName { get; set; }
    public string Surname { get; set; }
}

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }
}

Now, if I have a list of, for example, two persons - one with an address set and the other with a NULL address - an exception is thrown when generating the document:

System.InvalidOperationException: 'An error has been encountered while evaluating the expression or statement 'Address.Street]>'. Object reference not set to an instance of an object.'
InnerException: NullReferenceException: Object reference not set to an instance of an object.

If both persons have NULL addresses or if both have addresses set, there is no error.

In the options of the ReportingEngine, I have set the AllowMissingMembers option:

var reportingEngine = new ReportingEngine {
    Options = ReportBuildOptions.AllowMissingMembers | ReportBuildOptions.RemoveEmptyParagraphs
};

I have attached a solution with the example.
Aspose.JsonReportingEnging.zip (38.2 KB)

In my production scenario, the data model is significantly larger and more complex. Therefore, I want to avoid unnecessary instantiation of sub-objects to reduce data volume as well. Is there a way to achieve this?

@KiBa1231

The issue you’re encountering with the System.InvalidOperationException when one of the objects in your JsonDataSource is null is a common scenario when using the Aspose.Words LinqReportingEngine. When the engine tries to access properties of a null object, it throws an exception because it cannot evaluate the expression.

To handle this situation, you can utilize the AllowMissingMembers option in the ReportingEngine. This option allows the engine to skip over any missing members without throwing an exception. You have already set this option in your code, which is a good step.

Here’s a brief overview of how to implement this correctly:

  1. Ensure that you have set the AllowMissingMembers option when initializing the ReportingEngine:

    var reportingEngine = new ReportingEngine {
        Options = ReportBuildOptions.AllowMissingMembers | ReportBuildOptions.RemoveEmptyParagraphs
    };
    
  2. When defining your template, you can use conditional expressions to check for null values. For example, in your template, you can use something like:

    {{#if Address}}
    {{Address.Street}}
    {{/if}}
    

    This way, if the Address is null, it will not attempt to access Address.Street, thus avoiding the exception.

  3. If your data model is complex and you want to avoid unnecessary instantiation of sub-objects, consider using a more flexible data structure that can handle null values gracefully. This might involve using optional types or nullable types in your data model.

For more detailed information on handling missing members in the LinqReportingEngine, you can refer to the documentation on accessing missing members of data objects, which provides additional insights and examples.

By implementing these strategies, you should be able to generate your documents without encountering exceptions due to null values in your data source.

Unfortunately, this answer does not help. The AllowMissingMembers option is already set. If I remove this option, I get a completely different exception.

Furthermore, modifying the template to include NULL checks is not an option, as there are multiple templates provided by the customer.

We deliberately chose the JSON data source to keep the document generation type-agnostic.

What adjustments to the data model would be feasible in this case?

@KiBa1231

Have you tried changing <<[Address.Street]>> to <<[Address?.Street]>>?

Yes, that works. But why is the option AllowMissingMembers not working in that case?

@KiBa1231

ReportBuildOptions.AllowMissingMembers works as expected in this case. The option affects only those members that are completely missing, that is, not defined in the structure of data provided. This happens when, for example, none of list items contains members being accessed in a template. In your case, concerned members are defined for some list items (and hence, defined for a whole data source), but not filled for the rest, which is a completely different situation.

Ok, thanks for your explanation.

1 Like