1

I am getting a NullReferenceException in my code.

I call a method datecheck from another other page in the same project and pass two strings as parameters. These contain the date selected by user in string format. Now when i try to initialize 2 labels label1 and label2 with these string values I get the NullReferenceException.

Code lines showing error and stack trace is as follows:

Line 39:         public void datecheck(String s1, String s2)
Line 40:         {
Line 41:             Label1.Text = s1;
Line 42:             Label2.Text = s2;            
Line 43:         }

Source File: I:\Aditya\GuestHouse\GuestHouseApp\GuestHouseApp\Booking Status.aspx.cs Line: 41

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
GuestHouseApp.Booking_Status.datecheck(String s1, String s2) in I:\Aditya\GuestHouse\GuestHouseApp\GuestHouseApp\Booking Status.aspx.cs:41
GuestHouseApp.Booking.Button1_Click(Object sender, EventArgs e) in I:\Aditya\GuestHouse\GuestHouseApp\GuestHouseApp\Booking.aspx.cs:28
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +113
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5348

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Demon
  • 11
  • 2
  • You have not yet intialized `Label1` ... double check that. Simply assigning a value to the .Text property does not initialize the Label control – IAbstract Jul 02 '11 at 05:46
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 15 '14 at 03:08

2 Answers2

4

Pay attention to the error message and line numbers. Here is how to read the stack trace:

Object reference [is null and is] not set to an instance of an object [in method] GuestHouseApp.Booking_Status.datecheck [at file:line] Status.aspx.cs:41

If the compiled code is up to date then this means that the line Label1.Text = s1; is the line that raises the exception.

Furthermore, it can be deduced that Label1 is null because a NullReferenceException is raised when this happens: (anExpressionEvaluatingToNull).Member. The only member on an explicit receiver (which may be null) that is being accessed in that line is Text so then Label1 must be null.

Use the debugger if needed: then you can inspect the current variables and objects.

Happy coding.

  • Your explanation is good, but what if the Text-property of Label1 had something like `if(value == null) throw new NullReferenceException();`? Wouldn't that mean that the problem might be s1 beeing null? Though you'd probably get an extra line in the stacktrace. – Alxandr Jul 02 '11 at 12:14
  • @Alxandr: a `string` can be null - it is *loosely* handled much like that of an empty string. The only time this would be an issue is you were to perform a function that requires `(!String.IsNullOrEmpty(s1))` – IAbstract Jul 02 '11 at 12:22
  • @IAbstract: Yeah, I know, but that was my point. If that's the case, it might not be Label1 that's null. – Alxandr Jul 02 '11 at 12:24
  • @Alxandr: A `.Text` property will not care. It isn't trying to parse anything by default - only for display. If the OP had something else happening when the `.Text` property changes, then the exception would not be at `Label1`; or, the operation could result in an initialization exception. – IAbstract Jul 02 '11 at 12:26
  • @Alxandr If the exception was raised from inside the `Text` property this would have been reflected as being another level on the stack-trace (it would have started from the `Text` property setter). That's how it's possible to tell it originated from that line -- and not from inside `Text`. –  Jul 02 '11 at 17:09
0

I am guessing Label1 has not been initialized.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118