我编写的一个小论坛,使用cookies保存用户登录信息,可是却出现了问题,请热心人帮忙看看:
代码如下(Vb.net写的):
     sub Page_Load(sender as object,e as eventargs)
      dim userName
      userName=Request.Cookies("userInfo").value 
      if userName="" then
         userNameLabel.Text="客人,您还没有登录。请点击下面的登录按钮。"
         loginOutButton.Text="请点击这里登录"
      else
          userNameLabel.Text=userName
      end if
  end sub编译错误信息如下:
未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 9:    sub Page_Load(sender as object,e as eventargs)
行 10:       dim userName
行 11:       userName=Request.Cookies("userInfo").value 
行 12:       if userName="" then
行 13:          userNameLabel.Text="客人,您还没有登录。请点击下面的登录按钮。"
 源文件: c:\inetpub\wwwroot\WebTest\community\communityIndex.aspx    行: 11 
这是为什么啊?我曾经成功的执行过,不知现在怎么出现了问题?

解决方案 »

  1.   

    没有取得cookie的值(Request.Cookies("userInfo")==null)或者检查一下你的cookie是否禁用
      

  2.   

    你的Cookie是不是设的时间不够长呢?Dim objCookie As HttpCookie = New HttpCookie("userInfo")
    objCookie.Values.Add("UserName", Me.TextBox1.Text)
    objCookie.Expires = DateTime.Now.AddDays(1)              '过期期限设置为1天
    Response.AppendCookie(objCookie)
      

  3.   

    你应先判断cookies是否为null,如果为null,则出现楼主错误
    if(Request.Cookies("userInfo") != null)
    {
    userName=Request.Cookies("userInfo").value ;
    }
      

  4.   

    sub Page_Load(sender as object,e as eventargs)
             
          If Request.Cookies("userinfo") Is Nothing Then
             userNameLabel.Text="客人,您还没有登录。请点击下面的登录按钮。"
             loginOutButton.Text="请点击这里登录"
          else
              userNameLabel.Text=Request.Cookies("userinfo").Value
          end if
      end sub
      

  5.   

    呵呵,问题解决了,就是缺少判断cookies是否存在。
    谢谢,这里真是太好了,给分给分!