Foreach gives object reference not set with JSON Data Source using .NET | LINQ Reporting Engine

I have the following Json:

{
	"Id": 220000001,
	"Name": "Test RDU",
	"Commitments": [
		{
			"CommitmentId": 1,
			"General": {
				"StartDate": "2022-08-04T00:00:00",
				"EndDate": "2025-08-31T00:00:00",
				"Notes": {
					"Id": 4,
					"Contents": "<p>Notes from AGRITEAM</p>"
				}
			},
			"PaymentSchedule": {
				"ScheduleId": 1,
				"IsCompleted": "No",
				"ReservationMoment": 1,
				"FirstPayment": 1500.0
			},
			"ReportingObligations": {
				"ObligationId": 1,
				"IsCompleted": "No",
				"NotesId": 5,
				"Notes": {
					"Id": 5,
					"Contents": "<p>Obligations notes</p>"
				},
				"CreateDate": "2022-08-03T13:43:10.9935712"
			},
		},
		{
			"CommitmentId": 2,
			"General": {
				"StartDate": "2022-08-01T00:00:00",
				"EndDate": "2027-10-31T00:00:00",
				"Notes": null
			},
			"PaymentSchedule": null,
			"ReportingObligations": null,
		}
	],
	"RegistrationDate": "2022-08-01T11:47:47.733013",
	"MutationDate": "2022-08-03T14:15:01.5250712"
}

And the following template:

<<[Id]>>
<<[Name]>>
<<foreach [cm in Commitments]>>
	<<[cm.CommitmentId]]>>
	<<[cm.General.StartDate]>>
	<<[cm.General.EndDate]>>
	<<if [cm.PaymentSchedule !=null]>>
		<<cm.PaymentSchedule.ReservationMoment>>
	<</if>>
<</foreach>>

When using the ReportingEngine with JsonDataSource I get an error on cm.PaymentSchedule.ReservationMoment. The error is:
“An error has been encountered while evaluating the expression or statement ‘cm.paymentSchedule.reservationMoment]>’. Object reference not set to an instance of an object.”

Please help me…

@rdumee

Please make use of the ?. null-conditional operator in your template instead as follows:

<<[Id]>>
<<[Name]>>
<<foreach [cm in Commitments]>>
	<<[cm.CommitmentId]>>
	<<[cm.General.StartDate]>>
	<<[cm.General.EndDate]>>
		<<[cm.PaymentSchedule?.ReservationMoment]>>
<</foreach>>

The questionmark didn’t work.
If I add the following:

<<if [cm.PaymentSchedule != null]>>
Paymentschedule header
<</if>>

Then in both occasions the Paymentschedule header is shown.

@rdumee

The template in my previous reply relates to your original query, it prints ReservationMoment when PaymentSchedule is not null. The template has nothing to do with an if tag.

If your scenario requires using of an if tag exclusively, then you can use the following syntax instead:

<<if [cm.PaymentSchedule.Count() != 0]>>Not null.<<else>>Null.<</if>>

First the paymentschedule is not a collection so count won’t work.
Second, this is not the solution to my problem. How can I fix the template so it does not give the error on the second commitment where scheduledpayment is null?

@rdumee

I have checked this template using your data and was able to get expected results: It prints “Null” when PaymentSchedule is null (in JSON) and prints “Not null” otherwise. The reason why comparison with null does not work in this case and why you should use Count() instead is explained in this reply. Please check the template at your end and let us know if it does not work for you.

But have you tried it with the original template. With PaymentSchedule.ReservationMoment

@rdumee

I have checked the following template snippets using your data:

<<[cm.PaymentSchedule?.ReservationMoment]>>
<<if [cm.PaymentSchedule.Count() != 0]>><<[cm.PaymentSchedule?.ReservationMoment]>><</if>>
<<if [cm.PaymentSchedule.Count() != 0]>><<[cm.PaymentSchedule.ReservationMoment]>><</if>>

All of them produce expected results: Print “1” when PaymentSchedule is not null and print nothing otherwise.

Note that the template snippets are intended to be used within <<foreach [cm in Commitments]>> as per your original template.

Thanx, it works now.
I find it quite unusual, that I can’t apply the null equation and that I must use the Count.
I think this should be a topic in your documentation when using json as datasource.

@rdumee

It is good to know your issue is resolved. We will plan documentation improvement in this regard. Thanks for highlighting.