日期:2014-05-18  浏览次数:21428 次

System.NullReferenceException: Object reference not set to an instance of an obj
Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error: 


Line 18: protected void btnlogin_Click(object sender, EventArgs e)
Line 19: {
Line 20: if (txtCode.Text.Trim() != Session["verify"].ToString()//这里提示出错了
Line 21: {
Line 22: Response.Write("<script>alert('验证码错误');location='Login.aspx'</script>");
 

Source File: d:\程序\ExamOnLine\Login.aspx.cs Line: 20 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
  Login.btnlogin_Click(Object sender, EventArgs e) in d:\程序\ExamOnLine\Login.aspx.cs:20
  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) +5349

 


------解决方案--------------------
Session["verify"]不存在。
------解决方案--------------------
txtCode.Text 这个是null?
------解决方案--------------------
Session["verify"]可能为null值,就会报错。

先判断下Session["verify"]!=null