How to get rid of null reference exception error

5 posts, 1 answers
  1. Tursunhuja
    Tursunhuja avatar
    29 posts
    Member since:
    May 2018

    Posted 07 Jun Link to this post

    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" ?

     

  2. Tursunhuja
    Tursunhuja avatar
    29 posts
    Member since:
    May 2018

    Posted 12 Jun Link to this post

    Does someone have any opinion?
  3. Answer
    Todor
    Admin
    Todor avatar
    396 posts

    Posted 12 Jun Link to this post

    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
  4. Tursunhuja
    Tursunhuja avatar
    29 posts
    Member since:
    May 2018

    Posted 12 Jun in reply to Todor Link to this post

    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?

  5. Todor
    Admin
    Todor avatar
    396 posts

    Posted 17 Jun Link to this post

    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
Back to Top