-1

Ive build an UI for a Touchdisplay on Rpi with Windows 10 iot Core.

When I tested this on my PC it worked. If I now test the software on the Raspberry I get an exception on this line(s):

lblTime.Text = DateTime.Now.ToShortTimeString();
lblDay.Text = DateTime.Now.ToString("dddd");

Its an exception of type 'System.NullReferenceException' what means this DateTime.Now is null Right?

The time is set on the Windows Device Portal so idk why there ist no "Now"-Time

Daniel
  • 23
  • 2
  • 3
    I would be surprise that would be the case, `DateTime` is a non-nullable struct, so something would have to be `null` **within** the `DateTime` implementation and that is quite unlikely. I guess `lblTime` or `lblDay` is `null`. Set a breakpoint and see the values of the variables – Martin Zikmund Apr 15 '19 at 11:58
  • Post the full exception text that's returned if you call `Exception.ToString()`. I'm pretty sure you'll find that `lblTime` or `lblDay` are null. DateTime is a struct and thus *non*nullable. It's data is [a single `UInt64` tick count](https://referencesource.microsoft.com/#mscorlib/system/datetime.cs,138) and thus non-nullable as well – Panagiotis Kanavos Apr 15 '19 at 12:49

2 Answers2

1

I would guess, that lblTime and/or lblDay is null.

You can simply debug this by using a null check (if(lblTime == null) {)

Marvin Klar
  • 1,869
  • 3
  • 14
  • 32
  • 1
    "Yeah, it could possibly mean that Now is null" - no, it couldn't mean that as `DateTime` is a non-nullable value type. I agree with your expectation that it's more likely that `lblTime` or `lblDay` is null, but it's worth removing the first sentence from your answer - and fixing your null check to be in C#, as that's the language the OP is using. – Jon Skeet Apr 15 '19 at 12:06
0

I have now found the Problem. It was just to early(some first lines in the Main Page method), some Code later it worked fine.

Daniel
  • 23
  • 2