.NET Standard
A formal specification of .NET APIs that are available on multiple .NET implementations.
493 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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 ?
are there are another solution for solving that
error that display for me
is
error object reference not set to an instance of an object
This is the most common error in programming and the easiest to find. Typically the error message tells you exactly what object is null.
Set a break point and use the watch window to find the null object. Once you find what is null then backtrack through your code to figure out why the object is not set. If your logic allows or expects a null object, then use the null coalesce operator or null conditional operator.
?? and ??= operators (C# reference)
?: operator (C# reference)
if i need to check object reference
if (Object.ReferenceEquals(null, myObjVar))
{
.......
}
so how to make it please
can any one help me
You have to understand, the community cannot find the null objects because we do not have your data and cannot run your code. It is up yo you to run your code through the Visual Studio debugger to find what item(s) is null. Simply set a break point and find the null object(s).
First look at the Visual Studio Debugger
https://learn.microsoft.com/en-us/visualstudio/debugger/?view=vs-2022
For example "error" or "reason" could be null in the code below which will cause an exception.
This is the main reason I suggested the null conditional operator above.
Please do not mindlessly copy the code above. Make an effort to read the documentation!
Sign in to comment
2 answers
Sort by: Most helpful
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
can you show me what you mean by
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
Sign in to comment
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.
Sign in to comment
Activity