locked
Getting NullReferenceException in Session Is Nothing Check RRS feed

  • Question

  • User368742804 posted
                        If HttpContext.Current.Session("FirmID") Is Nothing Then
                            HttpContext.Current.Session("FirmID") = CStr(intFirmID)
                        End If

    Why is the above throwing a .NullReferenceException

      detail="System.NullReferenceException: Object reference not set to an instance of an object.
   at ...line 1862

    Line 1862 if the first line.

    I'm catching the exception with ELMAH but I'd like to prevent it and understand why it is occurring.

    Thanks

                        If HttpContext.Current.Session("FirmID") Is Nothing Then                        HttpContext.Current.Session("FirmID") = CStr(intFirmID)                    End If

    Wednesday, March 23, 2016 5:58 PM

Answers

  • User-986267747 posted

    Hi mmmtbig,

    mmmtbig

    If HttpContext.Current.Session("FirmID") Is Nothing Then HttpContext.Current.Session("FirmID") = CStr(intFirmID

    mmmtbig

    detail="System.NullReferenceException: Object reference not set to an instance of an object.
   at ...line 1862

    Are you sure that the code above cause this exception?

    mmmtbig

    Why is the above throwing a .NullReferenceException

    mmmtbig

    If HttpContext.Current.Session("FirmID") Is Nothing Then

    Besides, Note that the code in your edit is not checking that Session is not null. If it is null, the expression Session("FirmID") will still cause the error because this is searching the session instance for the "FirmID" key - so if you think about it logically you are looking for a key in an uninstantiated object, hence the null reference exception. Therefore you need to verify Session has been instantiated before checking if Session("FirmID") is null:

    If Not HttpContext.Current.Session Is Nothing
    Then
            If HttpContext.Current.Session("FirmID") Is Nothing Then
                       HttpContext.Current.Session("FirmID") = CStr(intFirmID)
            End If
    
    End If

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 24, 2016 5:40 AM

All replies

  • User753101303 posted

    Hi,

    Where is this code? Are you 100% sure it is never called when HttpContext.Current or HttpContext.Current.Session is Nothing? If you don't see this in VS could it be that the line number is not exact. If deploying PDB files for a release build, I believe the line number can be out of sync because of code optimization (looks like you get this in production but I'm a bit surprised that you do have a line number).

    Wednesday, March 23, 2016 6:15 PM
  • User-986267747 posted

    Hi mmmtbig,

    mmmtbig

    If HttpContext.Current.Session("FirmID") Is Nothing Then HttpContext.Current.Session("FirmID") = CStr(intFirmID

    mmmtbig

    detail="System.NullReferenceException: Object reference not set to an instance of an object.
   at ...line 1862

    Are you sure that the code above cause this exception?

    mmmtbig

    Why is the above throwing a .NullReferenceException

    mmmtbig

    If HttpContext.Current.Session("FirmID") Is Nothing Then

    Besides, Note that the code in your edit is not checking that Session is not null. If it is null, the expression Session("FirmID") will still cause the error because this is searching the session instance for the "FirmID" key - so if you think about it logically you are looking for a key in an uninstantiated object, hence the null reference exception. Therefore you need to verify Session has been instantiated before checking if Session("FirmID") is null:

    If Not HttpContext.Current.Session Is Nothing
    Then
            If HttpContext.Current.Session("FirmID") Is Nothing Then
                       HttpContext.Current.Session("FirmID") = CStr(intFirmID)
            End If
    
    End If

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 24, 2016 5:40 AM