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 ?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,421 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,212 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,317 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 57,241 Reputation points
    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