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.
Related
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 }
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 4 years ago.
When filtering a list using lambda where and contains, I get a NullReference error
var servers = _context.Servers.ToList();
servers = servers.Where(t => t.Technology.Contains(technology)).ToList();
But I get a this error:
Exception thrown: 'System.NullReferenceException' in
ServerBuildApp.dll ServerBuildApp.Models.Servers.Technology.get
returned null.
The list of servers contains a 'Technology' property and it contains the string I am passing it, for example "BIZ"
Any ideas? Or am I doing this completely wrong?
You need to check the t.Technology for null.
var servers = _context.Servers.ToList();
servers = servers.Where(t => t.Technology?.Contains(technology) is true).ToList();
You should check t.Technology for null and then check it contains.
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 5 years ago.
I am trying to compare current date with a calculated date from offset as shown below. The line throws an exception, enters catch block but exception is null. I cannot find what is the exception because of it
DateTime.UtcNow.Date.CompareTo(new UtcDateTime(startDate.AddDays(activity.DueInDaysOffset.GetValueOrDefault()))) > 0
startDate is date from DB
activity.DueInDaysOffset is an integer - positive/negative.
UtcDateTime is a class from 'Microsoft.CommonDataService' used for conversion.
What could be the issue?
Without knowing the rest of your code, I would guess that either startDate or activity is null. Verify that both variables have values.
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);
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