How can DateTimeFormatInfo.CurrentInfo be null
Asked Answered
B

1

6

I have the following code in my C# application.

DateTimeFormatInfo.CurrentInfo.DayNames

ReSharper 7.1.1 is highlighting the fact that the DateTimeFormatInfo.CurrentInfo could cause a null reference exception.

Under what circumstances would this occur? Or is this just a mistake on ReSharper's part believing that any object whose property you access should be null checked?

Babushka answered 4/6, 2013 at 18:2 Comment(0)
A
9

ReSharper is most probably just doing lexical analysis here and nothing deeper.

Since DateTimeFormatInfo is a class, a variable of this type can be null. Which means that the instance returned by DateTimeFormatInfo.CurrentInfo can be a null reference.

That's the error you are getting.

ReSharper doesn't understand that the method was coded such that it will not return a null reference, so it gives a warning.

Don't take the messages from ReSharper as scripture...

Annelleannemarie answered 4/6, 2013 at 18:6 Comment(5)
You make an excellent point. I wanted to make sure I understood why this was the case.Babushka
I don't know what the correct answer is, but I'm pretty sure this answer is incorrect. ReSharper is much too smart to just flag all situations where a property returns an object reference as a possible null reference error - if it did that all programs would get hundreds of these errors. As a specific counter-example I can point out that use of DateTimeFormatInfo.InvariantInfo.DayNames does not get flagged as a possible null reference error.Rayon
Someone should query JetBrains to get the correct answer. Maybe it's a bug in version 7.1. Or maybe it's possible for some strange fringe cases where the current culture info has been set to a programmer-created culture that DateTimeFormatInfo.CurrentInfo can actually return null.Rayon
It's interesting that DateTimeFormatInfo.Current is flagged as possibly null but CultureInfo.CurrentCulture.DateTimeFormat is not.Collegiate
FWIW... here's an example: 0 app 0x0169994a GetStackFrames () (vector:368) 1 app 0x0167d961 Raise (Il2CppException) (Exception.cpp:30) 2 app 0x0167da3d RaiseNullReferenceException (StringView<char16_t>) (Exception.cpp:61) 3 app 0x0167da31 RaiseNullReferenceException () (Exception.cpp:56) 4 app 0x00af00b1 DateTimeFormatInfo_get_CurrentInfo_m1648708929 (Il2CppObject, MethodInfo) (il2cpp-codegen.h:342) 5 app 0x00aefed9 DateTimeFormatInfo_GetInstance_m3133223810 (Il2CppObject, Il2CppObject, MethodInfo) (Bulk_mscorlib_2.cpp:8800) ...Gasometry

© 2022 - 2024 — McMap. All rights reserved.