How to solve error object reference not set to an instance of an object ?

ahmed salah 3,216 Reputation points
2022-08-14T18:23:56.653+00:00

I work on csharp i get error object reference not set to an instance of an object

code give me error

var Data = result.items.Select(e => new { PartID = e.create._id, IsUpdated = e.create.error != null && e.create.error.reason !=null && !e.create.error.reason.Contains("document already exists") ? 0 : 1, ErrorStatus = e.create.error == null  | (e.create.error.reason!=null && e.create.error.reason.Contains("document already exists")) ? null : e.create.error.reason }).ToList();  

ElasticPartialUpdat.Create.error.get returned null.

so how to solve error please ?

Developer technologies ASP.NET ASP.NET Core
Developer technologies .NET Other
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-08-15T20:27:59.233+00:00

    you will need to determine which value is null:

    in the caller it can be one of:

    result
    result.items

    in the callback one of:

    e
    e.create


  2. Deba Khadanga 1 Reputation point
    2022-08-16T07:17:30.547+00:00

    Use "||" instead of "|". "|" evaluates both operands for final result. In your case , the 2nd operand of "|" i.e e.create.error.reason is throwing object reference not set error.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.