nullreferenceexception in visual studio [duplicate]
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
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.
I am guessing Label1 has not been initialized.
精彩评论