在select case语句上抛出NullReferenceException。有什么想法吗?

时间:2011-09-06 19:46:04

标签: .net asp.net vb.net

在生产中偶尔会出现此错误,但我无法在DEV中重现它

System.NullReferenceException: Object reference not set to an instance of an object.     
at CommandCenterHeader.ascx.vb:line 23     
at System.EventHandler.Invoke(Object sender, EventArgs e)     
at System.Web.UI.Control.OnLoad(EventArgs e)     
at System.Web.UI.Control.LoadRecursive()     
at System.Web.UI.Control.LoadRecursive()     
at System.Web.UI.Control.LoadRecursive()     
at System.Web.UI.Control.LoadRecursive()     
at System.Web.UI.Control.LoadRecursive()     
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

CommandCenterHeader.ascx.vb:第23行是“Select Case Me.CurrentUser.UserType.ToLower”

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Select Case Me.CurrentUser.UserType.ToLower
            Case "e", "f"
                lbtnccadmin.Visible = True
                lbtnccadmin.NavigateUrl = "/commandcenter/cc_admin.aspx"
            Case Else
                lbtnccadmin.Visible = False
        End Select
    End If
End Sub

<ComponentModel.Browsable(False)> _
    Protected ReadOnly Property CurrentUser() As CypStdLib.FL.QuotesDB.tblUserInfo
        Get
            If (Not IsNothing(Request.Cookies(cookieKey))) AndAlso (Not IsNothing(Request.Cookies(cookieKey)("userid"))) Then
                Dim uid As String = Request.Cookies(cookieKey)("userid") & ""
                If uid <> "" AndAlso IsNumeric(uid) Then
                    Dim curruser As New CypStdLib.FL.QuotesDB.tblUserInfo
                    If curruser.LoadByPrimaryKey(CLng(uid)) Then

                        Session("CurrentUser") = curruser
                        Return curruser
                    End If
                End If
            End If

            Return Nothing
        End Get
        'Set(ByVal value As CypStdLib.FL.QuotesDB.tblUserInfo)
        '    Current.Session("CurrentUser") = value
        'End Set
    End Property

3 个答案:

答案 0 :(得分:1)

显然 层次结构Me.CurrentUser.UserType.ToLower中的对象 一片空白。 MeMe.CurrentUserMe.CurrentUser.UserType

如果你不能想到为什么它可以为null或者什么可能是null ..我可以在切换案例之前添加一个log msg并将其存储在文件或数据库中并将此代码发送到生产测试目的(有时直到你弄清楚什么是错的)。 这会让你知道出了什么问题

答案 1 :(得分:0)

嗯,CurrentUser是否为空?如果没有,CurrentUser.UserType是否为空?

此外,ToLower需要作为方法调用... ToLower()

答案 2 :(得分:0)

当用户会话在Web应用程序中超时时,是否会发生这种情况?