This is a migrated thread and some comments may be shown as answers.

How to get rid of null reference exception error

4 Answers 408 Views
Report Designer (standalone)
This is a migrated thread and some comments may be shown as answers.
Tursunkhuja
Top achievements
Rank 2
Iron
Iron
Veteran
Tursunkhuja asked on 07 Jun 2019, 11:52 AM

Hello,

I'm using WebServiceDataSource to connect with database through odata. In database there is InventoryDocDetail table which has Product property referenced to Product table.

I'm connecting to InventoryDocDetail and printing the product's Sku property on my report. The problem here is InventoryDocDetail.Product may be null. In this case, I just want to print nothing. Currently it gives null reference exception error(See attached Error.JPG file).

I saw many forums, but no one helped me. For example, I wrote like this expression in textbox: = iif(Fields.Product is null, "this is null", Fields.Product.Sku), but it still showing error when product is null.

     1. How can we solve the issue?

     2. Can we use C# expression here like this: "= Fields.Product?.Sku" ?

 

4 Answers, 1 is accepted

Sort by
0
Tursunkhuja
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 12 Jun 2019, 05:40 AM
Does someone have any opinion?
0
Accepted
Todor
Telerik team
answered on 12 Jun 2019, 06:55 AM
Hi Tursunhuja,

You may use operators where only one of the operands gets evaluated depending on the condition, hence if the other one is null this will not result in an exception. With functions all their arguments get evaluated, and then the function gets executed. Currently, our Expression engine does not support syntax like "Fields.Product?.Sku".

For example, with the "?:" operator (check Logical/Bitwise operators) the expression may look like :

= IIf(CStr(Fields.Product) = "", 0, 1) = 1 ? Fields.Product.Sku : "this is null"

You may test also with the "??" operator. For example, you may check whether a Field is null with an expression like :

= Fields.Product ?? "this is null"

Regards,
Todor
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Tursunkhuja
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 12 Jun 2019, 07:43 AM

Hi,

I tried your example above, but still showing error related with JsonDataObject. See attached picture. 1st picture is about printing product's sku with 2 ways. 2nd picture is result of those example. So, what do you suggest in my case?

0
Todor
Telerik team
answered on 17 Jun 2019, 07:28 AM
Hello Tursunhuja,

I have made a mistake. Note that IsNull requires two arguments. There are also known issues with the JsonDataSource - check JsonDataSource evaluates NULL values incorrectly and Check with "?" in the expression for null values items in our Feedback portal. Here is a workaround I will suggest :

= IIf(CStr(Fields.Product) = "", 0, 1) = 1 ? Fields.Product.Sku : "this is null"

I have attached a sample report to demonstrate the approach.
I have corrected the expression also in my previous reply so that our users do not get confused by it.

Regards,
Todor
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Report Designer (standalone)
Asked by
Tursunkhuja
Top achievements
Rank 2
Iron
Iron
Veteran
Answers by
Tursunkhuja
Top achievements
Rank 2
Iron
Iron
Veteran
Todor
Telerik team
Share this question
or