日期:2014-05-19  浏览次数:20597 次

System.NullReferenceException: 未将对象引用设置到对象的实例未将对象引用设置到对象的实例。请各位指点!!
未将对象引用设置到对象的实例。  
说明:   执行当前   Web   请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。  

异常详细信息:   System.NullReferenceException:   未将对象引用设置到对象的实例。

源错误:  


行   20:   private   void   Page_Load(object   sender,   System.EventArgs   e)
行   21:   {
行   22:   string   userName=Request.Form[ "userName "].ToString();
行   23:   string   userPwd=Request.Form[ "userPwd "].ToString();
行   24:   //   在此处放置用户代码以初始化页面
 

源文件:   d:\bysjzl\bysjcg\login.aspx.cs         行:   22  

堆栈跟踪:  


[NullReferenceException:   未将对象引用设置到对象的实例。]
      bysjcg.login.Page_Load(Object   sender,   EventArgs   e)   in   d:\bysjzl\bysjcg\login.aspx.cs:22
      System.Web.UI.Control.OnLoad(EventArgs   e)
      System.Web.UI.Control.LoadRecursive()
      System.Web.UI.Page.ProcessRequestMain()

附上我的源程序:
using   System;
using   System.Collections;
using   System.ComponentModel;
using   System.Data;
using   System.Drawing;
using   System.Web;
using   System.Web.SessionState;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.HtmlControls;
using   System.Data.SqlClient;

namespace   bysjcg
{
///   <summary>
///   login   的摘要说明。
///   </summary>
public   class   login   :   System.Web.UI.Page
{
private   void   Page_Load(object   sender,   System.EventArgs   e)
{
string   userName=Request.Form[ "userName "].ToString();
string   userPwd=Request.Form[ "userPwd "].ToString();
//   在此处放置用户代码以初始化页面

SqlConnection   con=new   SqlConnection( "server=.;database=gygl;uid=sa;pwd=; ");
con.Open();
SqlCommand   cmd=new   SqlCommand( "select   count(*)   from   Users   where   userName= ' "+userName+ " '   and   userPwd= ' "+userPwd+ " ' ",con);
int   count=Convert.ToInt32(cmd.ExecuteScalar());
if(count> 0)
{
Response.Redirect( "main.aspx ");
}
else
{
Response.Redirect( "loginfail.html ");
}
}

#region   Web   窗体设计器生成的代码
override   protected   void   OnInit(EventArgs   e)
{
//
//   CODEGEN:   该调用是   ASP.NET   Web   窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

///   <summary>
///   设计器支持所需的方法   -   不要使用代码编辑器修改
///   此方法的内容。
///   </summary>
private   void   InitializeComponent()
{        
this.Load   +=   new   System.EventHandler(this.Page_Load);

}
#endregion
}
}

 


------解决方案--------------------
Request.Form[ "userName "] 为null 判断一下
string userName=Request.Form[ "userName "]==null? " ":Request.Form[ "userName "];
------解决方案--------------------