DateTime CompareTo operation throwing error [duplicate] - c#

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.

Related

C#: How to find difference of days between 2 nullable datetimes? [duplicate]

This question already has answers here:
C# - Get date difference of Nullable DateTime in int
(2 answers)
How to subtract 2 nullable DateTimes in C#? [closed]
(3 answers)
Closed 3 years ago.
A similar question was posted here in regards to calculating the difference in days of 2 datetimes, however both of my datetimes are nullable, which prevents me from using "TotalDays" as is suggested in that question.
DateTime? startDate;
DateTime? endDate;
return(d1-d2).TotalDays; //This won't work
Any idea how to fix this?
Thanks
You can do this:
if (startDate.HasValue && endDate.HasValue)
{
return (startDate.Value - endDate.Value).TotalDays;
}
else
{
// handle one or more dates being null
}
If the value it's null that will throw an exeption,
to prevent this just add a "?" like this:
return(d1-d2)?.TotalDays;
So if the object is null it won't go any further

Why is this throwing System.FormatException:String was not recognized as a valid DateTime [duplicate]

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
Closed 5 years ago.
Why does this:
Convert.ToDateTime("08/31/2017")
throw an System.FormatException but not this:
Convert.ToDateTime("09/12/2017")
If you need more information please ask and I will update or comment. I have no clue what is causing this issue, so I don't know what details you need.
The default order for this date format on your computers culture is Day/Month/Year, as 31 is not a valid month it fails. If you want this order, you need to provide the format with it:
var x = DateTime.ParseExact("08/31/2017", "MM/dd/yyyy",CultureInfo.InvariantCulture);

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# Android Unhandled Exception Error System.NullReferenceException [duplicate]

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 get Database Data to insert into List View
Error Code:
Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object. occurred
UPDATE 1:
Either shMsg or sqldb is null.
Put a breakpoint on line shMsg = FindViewById<TextView> (Resource.Id.shMsg);Then check if your resource is filled, and if you step 1 execution further, if the variable shMsg has a value.
Then proceed stepping until you realise which object has a null value.
The reason you get this exception is because you're trying to get a property of something that doesn't exist, because it's null.
If the sqldb variable is null, make sure you don't dispose of the connection to the database prematurely, which is a common mistake.

Why is this a valid comparison [duplicate]

This question already has answers here:
C# okay with comparing value types to null
(11 answers)
Closed 7 years ago.
Here is some example code:
static DateTime time;
if (time == null)
{
/* do something */
}
Since DateTime cannot be null, why does this code compile?
Edit:
The issue is not just that this code will always return false,but why something like DateTime which is never null is allowed in such a comparison.
Although time is of a non-nullable value type, it can be converted to nullable and compared to null. The comparison would yield false, which is a valid outcome.
This does not mean, however, that it is a good code. Tools, such as re:sharper, would flag this line with a warning saying "Expression is always false".

Categories