同一目录下,一session.ascx控件代码如下
:public class cj_session : System.Web.UI.UserControl
{ private int m_intType;
public  int type
{
set
{
m_intType=value;
}
get
{
return m_intType;
}
}
   
private void Page_Load(object sender, System.EventArgs e)
{

}
public void judge()
{
bool isUser; 
string m_strName="";
if(Session["username"]!=null)
{
m_strName=Session["username"].ToString();
users objSession=new users();
if(m_strName!="")
{
isUser=objSession.IsExist(m_strName);
if(isUser)
{
if(type==objSession.type){;} }

}

}
else
{
Response.Redirect("after_login.aspx");
}
}
在同一目录下a.aspx也面拉控件到也面 后台引用代码 cj_session objsession=new cj_session();
objsession.type=m_type;
objsession.judge();
运行错误提示:未将对象引用到实例
未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 39:  bool isUser; 
行 40:  string m_strName="";
行 41:  if(Session["username"]!=null)
行 42:  {
行 43:  m_strName=Session["username"].ToString();
 源文件: c:\inetpub\wwwroot\test4\afterbehind\cj\cj_session.ascx.cs    行: 41 堆栈跟踪: 
[NullReferenceException: 未将对象引用设置到对象的实例。] 
为什么?????????

解决方案 »

  1.   

    目录a的login。aspx添加session到session['username'] 能在a\bb下的文件session.ascx中引用login中添加的session吗?
      

  2.   

    先在Global.asax中定义session["username"]
    if(Session["username"]!=null)改为if(Session["username"].ToString()!="")试试
      

  3.   

    用HttpSessionState将Session当做参数传进来再使用就可以了,这样当然找不到Session了.
      

  4.   

    我怎觉得没错了
    你在把if(Session["username"]!=null)
    行 42:  {
    行 43:  m_strName=Session["username"].ToString();
     
    以下的代码贴出来看看?
      

  5.   

    行 39:  bool isUser; 
    行 40:  string m_strName="";
    行 41:  if(Session["username"]!=null)
    行 42:  {
    行 43:  m_strName=Session["username"].ToString();
     源文件: c:\inetpub\wwwroot\test4\afterbehind\cj\cj_session.ascx.cs    行: 41 堆栈跟踪: 
    [NullReferenceException: 未将对象引用设置到对象的实例。]
       System.Web.UI.UserControl.get_Session()
       test4.AfterBehind.cj.cj_session.judge() in c:\inetpub\wwwroot\test4\afterbehind\cj\cj_session.ascx.cs:41
       test4.AfterBehind.cj.cj_welcome.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\test4\afterbehind\cj\cj_welcome.aspx.cs:28
       System.Web.UI.Control.OnLoad(EventArgs e)
       System.Web.UI.Control.LoadRecursive()
       System.Web.UI.Page.ProcessRequestMain() 
    --------------------------------------------------------------------------------
    版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
      

  6.   

    to 回复人: panzhefeng(Funpan) ( ) 信誉:100 
    为什么这样就找不到了呢 是方法judge()找不到吗?可我把判断变量在page-load中就能判断
      

  7.   

    if(Session["username"]!=null)改为if(Session["username"].ToString()!="")试试
      

  8.   

    if(Session["username"]!=null)
    改成 string name=(string)Session["username"];
         if(name!=null)
         …………
    试试