“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 20:         {
行 21:           
行 22:                 if (Session["logon"].ToString() != "logonSuccess")行 23:                     Response.Redirect("logon.aspx");
行 24:           
 源文件: C:\wwwroot\Session\Session\index.aspx.cs    行: 22 堆栈跟踪: 
[NullReferenceException: 未将对象引用设置到对象的实例。]
   Session.index.Page_Load(Object sender, EventArgs e) in C:\wwwroot\Session\Session\index.aspx.cs:22
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
   System.EventHandler.Invoke(Object sender, EventArgs e) +0
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061
请问此错误怎么解决?
这是书上的一个例子,就是使用SESSION来保存客户信息的。
错误说是未将对象引用设置到对象的实例。  在红色的那行出错,我看了下代码,好像没有新建SESSION对象,怎么对SESSION对象初始化呢?
 

解决方案 »

  1.   

    没有一个叫logon的session.
    可以写成
    if(Session["logon"] !=null & Session["logon"].ToString()!="logonSuccess")
      

  2.   

    Session["logon"]未定义
     if (Session["logon"] != null)
    {
     if (Session["logon"].ToString() != "logonSuccess")
    ....
    }
      

  3.   

    有可能是Session["logon"]  为空造成的
      

  4.   

    Session["logon"]==null?"":Session["logon"]ToString() 
      

  5.   

    您可以写一个属性: public Boolean LoginOk
            {
                get
                {
                    if (Session["LoginOk"] != null && (bool)Session["LoginOk"])
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                set
                {
                    Session["LoginOk"] = value;
                }
            }
    在登录验证成功时,可以这样写:
    LoginOK = true;
    然后在其他页面的Page_load中:
    if (!loginOK)
    {
     Response.Redirect("logon.aspx"); 
    }更多参考:
    http://www.cnblogs.com/insus/articles/1429434.html
      

  6.   

    行 20:         {
    行 21: 
    行 22:             if (Session["logon"] != null & Session["logon"].ToString() != "logonSuccess")
    行 23:                     Response.Redirect("logon.aspx");
    行 24:           
     源文件: C:\wwwroot\Session\Session\index.aspx.cs    行: 22 堆栈跟踪: 
    [NullReferenceException: 未将对象引用设置到对象的实例。]
       Session.index.Page_Load(Object sender, EventArgs e) in C:\wwwroot\Session\Session\index.aspx.cs:22
       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
       System.EventHandler.Invoke(Object sender, EventArgs e) +0
       System.Web.UI.Control.OnLoad(EventArgs e) +99
       System.Web.UI.Control.LoadRecursive() +47
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061 
      

  7.   

    Session["logon"]   你这个没定义吧。
      

  8.   

    index.aspx.cs
    using System;
    using System.Data;
    using System.ComponentModel;
    using System.Drawing;
    using System.Web.SessionState;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;namespace Session
    {
        public partial class index : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {            if (Session["logon"] != null & Session["logon"].ToString() != "logonSuccess")
                        Response.Redirect("logon.aspx");
              
            }       
            private void InitializeComponent()
            {
                this.Load += new System.EventHandler(this.Page_Load);        }        protected override void OnInit(EventArgs e)
            {
                InitializeComponent();
                base.OnInit(e);
            }            }
    }
      

  9.   

    logon.aspx.cs
    using System;
    using System.Data;
    using System.ComponentModel;
    using System.Drawing;
    using System.Web.SessionState;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;namespace Session
    {
        public partial class logonForm : System.Web.UI.Page
        {        protected System.Web.UI.WebControls.Label Label1;
            protected System.Web.UI.WebControls.Label Label2;
            protected System.Web.UI.WebControls.TextBox txtUserName;
            protected System.Web.UI.WebControls.TextBox txtPassword;
            protected System.Web.UI.WebControls.Button btnLogon;
            protected void Page_Load(object sender, EventArgs e)
            {
             
            }        private void InitializeComponent()
            {
                this.btnLogon.Click += new System.EventHandler(this.btnLogon_Click);
                this.Load += new System.EventHandler(this.Page_Load);
            }
            protected override void OnInit(EventArgs e)
            {
                InitializeComponent();
                base.OnInit(e);
            }        private void btnLogon_Click(Object sender, System.EventArgs e)
            {
                if (txtUserName.Text == "user" && txtPassword.Text == "pass")
                {
                    Session["logon"] = "logonSuccess";
                    Response.Redirect("index.aspx");
                }
                else
                {
                    Session["logon"] = "logonFailed";
                }        }    }}
      

  10.   

    session的用法是这样的,
    session.add("名字",值(这个值就是名字的值))
    你看下你的session添加的是否正确
      

  11.   

    书上说logon.aspx是主页面。
    会不会和这个有什么关系?
    我并不知道在我建的这个项目里index.aspx和logon.aspx到底哪个是主页面
      

  12.   

    OK了  运行成功了
    protected void Page_Load(object sender, EventArgs e)
            {            if (Session["logon"] != null & Session["logon"].ToString() != "logonSuccess")
                        Response.Redirect("logon.aspx");
                else
                        Response.Write("OK");
            }
    可还有个问题,为什么登陆成功后跳转到index.aspx页面时屏幕输出为OKOK
    为什么输出了两次? 代码里只是Response.Write("OK");啊
      

  13.   

    你页面之间有没有传参数之类的啊,如果有记得传出参数的页面要设为首页才行,你的这个问题应该是Session["logon"]对象是空的,你加个断电调试看看,如果为空就仔细检查下
      

  14.   

    对的 以前每次我是在index.aspx界面时点开始运行,所以每次都提示SESSION
    我选中logon.aspx选项卡,再点开始运行就没问题了现在的问题就是那个OK为什么出现两次呢?
      

  15.   


    这个调试一下呀.看看Response.Write("OK"); 执行了几次