C#.Net Linq query and null object [duplicate] - c#

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 6 years ago.
I have a problem with my linq query. I get a null reference exception. What's wrong with my code? I do null checks but still get an exception.
What am I doing wrong?
Math.Round(invoiceData?.Lines?.Sum(x => x.Amount) ?? 0, 2);
Thanks in advance.

Have you tried to do like:
Math.Round(invoiceData?.Lines?.Sum(x => x.Amount ?? 0), 2);

Related

My problem is System.NullReferenceException: Object reference not set to an instance of an object [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 1 year ago.
When I am inputting wrong username and password this error shows
this is my code
RegLog reuser = res.ResultAs<RegLog>(); //database result <-- Error Occurs here.
any solution? I already tried to declare it as var but error still persist.
Thank you.
It is because for some reason (probably wrong username and password), the variable res is null.
Try to check a null reference before using res.
Example: if(res == null) { //do something }

Unity C#) there is NullReferenceException when i use OnTriggerEnter and i dont know how to fix it [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Why does ReSharper not suggest using null propagation for both of these blocks?
(2 answers)
Closed 2 years ago.
this is the problem when i use OnTriggerEnter and call the function there.

c# NullReferenceException in linq query [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 5 years ago.
In my code:
decimal maxPrice = list.Max(i => i.price);
Getting error - Object reference not set to an instance of an object.
NullReferenceException was unhandled by code.
The i value becomes null, though the list count is 6709. How do I resolve this?
So your list contains nulls.
Either filter them: list.Where(l => l != null).Max(...), or prevent the nulls to end up in the list in the first place.

c# object reference not set to an instance of an object For my c# [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
Can anybody help me? I keep getting this error "object reference not set to an instance of an object" on line 2150 in my coding here:
Ln 2148 if (Session.CharacterInfo.Timer > 0)
Ln 2149 {
Ln 2150 Session.SendData(RoomChatComposer.Compose(Actor.Id, "You have " + Session.CharacterInfo.Timer + " minutes left until you're paid", 0, ChatType.Whisper));
2151 }
One of the following:
Session is null
Session.CharacterInfo is null
Actor is null
RoomChatComposer is null (this could be calling a static method, if so discard)
ChatType is null (this most likely is enum? if so discard this)
Introduction to debugging

NullReference Exception in C# [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
I'm getting System.NullReferenceException: Object reference not set to an instance of an object. Exception with below code.
string avblCount = "0";
if (!string.IsNullOrWhiteSpace(item.PART_AVAILABILITY.AVAIL_COUNT.ToString() as string))
{
avblCount = item.PART_AVAILABILITY.AVAIL_COUNT.ToString();
}
Exception occurred from this line.
if (!string.IsNullOrWhiteSpace(item.PART_AVAILABILITY.AVAIL_COUNT.ToString() as string))
How can i fix this ?
One or more of these entities are null:
item
item.PART_AVAILABILITY
item.PART_AVAILABILITY.AVAIL_COUNT
You should check which one is, and then act accordingly

Categories