0

I am reading field values which may have an unformatted date in them or whitespace, i need to convert the string in the filled fields to datetime and write it back. I'm trying the following but get the error "Nullable object must have a value":

DateTime? localVersion = null;
DateTime? serverVersion = null;

if(!string.IsNullOrWhiteSpace(item.cellValueLocal))
{
    localVersion = DateTime.ParseExact(item.cellValueLocal, "ddMMyyyy", System.Globalization.CultureInfo.InvariantCulture);
}

if (!string.IsNullOrWhiteSpace(item.cellValueServer))
{
    serverVersion = DateTime.ParseExact(item.cellValueServer, "ddMMyyyy", System.Globalization.CultureInfo.InvariantCulture);
}

localVersion.Value.ToString("dd.MM.yyyy", CultureInfo.InvariantCulture);
serverVersion.Value.ToString("dd.MM.yyyy", CultureInfo.InvariantCulture);

Can anyone offer any guidance on what the issue might be here please?

8
  • Which line actually errors though? Commented Jun 21, 2016 at 14:01
  • Try to set them to a default value such as DateTime? localVersion = new DateTime(2015, 1, 18);' Commented Jun 21, 2016 at 14:01
  • 5
    What do you expect to happen if (say) item.cellValueLocal is null or whitespace? Commented Jun 21, 2016 at 14:01
  • 1
    Think about what @JonSkeet is saying. If the value is a string or whitespace, localVersion and serverVersion won't have a value you can call toString() on. Commented Jun 21, 2016 at 14:04
  • 2
    Additionally, what do you expect the last lines to achieve when they do work? You're calling ToString(), but not doing anything with the return value. Commented Jun 21, 2016 at 14:04

3 Answers 3

3

If your cellValue is null (or whitespace), your DateTime instances will not be initialized and still be set to null. You cannot access .Value on a nullable instance without a value. You have to check with .HasValue first:

if (localVersion.HasValue) {
  localVersion.Value.ToString(...);
}
Sign up to request clarification or add additional context in comments.

2 Comments

This won't actually do anything since you're not assigning the result of the ToString call to a variable.
@ChrisDunaway: yes. Execute additional code as needed …
1

The lines:

localVersion.Value.ToString("dd.MM.yyyy", CultureInfo.InvariantCulture);
serverVersion.Value.ToString("dd.MM.yyyy", CultureInfo.InvariantCulture);

Are errors. If item.cellValueLocal or item.cellValueServer is null or empty then localVersion.Value and serverVersion.Value will never be assigned a non-null value, and therefore Value will throw an exception when you access it.

You need to replace the operations with:

if (localVersion.HasValue)
    localVersion.Value.ToString("dd.MM.yyyy", CultureInfo.InvariantCulture);

Alternatively you could use the null coalescing operator:

DateTime version = localVersion ?? DateTime.Now;

Replacing DateTime.Now with an appropriate default value. In C# 6 you can use the ?. syntax to simplify it further to:

string version = localVersion?.ToString("dd.MM.yyyy", CultureInfo.InvariantCulture) ?? String.Empty;

Which does the null check inline.

2 Comments

Actually Value isn't null in that case it's just that the Nullable<T> type guards access to Value based on HasValue being false.
@juharr - Fair enough, I changed the wording.
0

If you are compiling the latest version of c# (6.0) compiler then you could shorten this further to be

localVersion?.ToString("dd.MM.yyyy", CultureInfo.InvariantCulture)

1 Comment

This will do nothing as you're not assigning the result of the ToString call to a variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.