views:

20

answers:

2

Hi All,

I've got an ASP.Net application, which starts throwing NUllReferenceExceptions, after a long period of running. The code in question is used early on in each individual session, where we're trying to establish some kind of referrer information. The thing is, I can't fathom out what can throw this exception.

The method in question (topmost in the stack trace) is:

Private Function ResolveReferrer(ByVal wrRequest As HttpRequest) As Referral

  '1) If we don't find a domain, try and get a match on any query strings
  If wrRequest.QueryString.Count > 0 Then
    For Each item As Referral In Me
      For Each sKey As String In wrRequest.QueryString.Keys
        If Not sKey Is Nothing AndAlso item.Names.Contains(sKey.ToLower) Then
          Return item
        End If
      Next sKey
    Next item
  End If


  Dim strSubDomain As String = Utility.RequestSubDomain(wrRequest.Url)
  '2) If we don't find one on the domain, see if we can find the domain in query string
  If Not wrRequest.QueryString.Item("domain") Is Nothing Then
    strSubDomain = wrRequest.QueryString.Item("domain")

    strSubDomain = HttpUtility.UrlDecode(strSubDomain)

    ' OK found a "domain" query string, so make up a referrer object to return
    ' ... just use the domain we've found for all the parameters
    Dim oRef As New Referral(strSubDomain, strSubDomain, strSubDomain)
    Return oref
  End If


  '3) If no query string of "domain", then see if the referring field is presented by the browser
  If Not wrRequest.UrlReferrer Is Nothing Then
    Dim sURL As String = wrRequest.UrlReferrer.ToString
    strSubDomain = Utility.RequestSubDomain(wrRequest.UrlReferrer)

    Dim oRef As New Referral(sURL, sURL, strSubDomain)
    Return oRef
  End If


  '4) See if we can find the domain defined in the web.config
  For Each item As Referral In Me

    ' See if we can find a referrer from the domain name
    If String.Compare(strSubDomain, item.FromDomain, False) = 0 Then
      Return item
    End If

  Next item


  '5) If we still can't find one, make one up with a value of "Unknown"
  Return New Referral("Unknown", "Unknown", "Unknown", "Unknown")
End Function

The class that this is a part of inherits from ArrayList. I've checked, and the only things added to this ArrayList are instances of the Referral class (which has multiple constructors, all simple).

What we do know is that, we can have a request that comes in with no referrer information, and it causes the exception to be thrown. At the same time, if a request with a referrer comes in, it works fine. In neither case is anything passed in the query string (so I think you can skip down to the '3 comment.

So, my question is, what in this method can cause a NullReferenceException to be thrown? If you need additional snippets added, or class definitions, just shout.

Utility.RequestSubDomain has reasonable complexity, so I doubt that it's being inlined and removed from the stack trace. And it's top is:

Public Shared Function RequestSubDomain(ByVal uri As System.Uri) As String
    If uri Is Nothing Then
        Return ""
    End If

Any help, or suggestions for finding more information would be appreciated. It's obviously (as with so many problems) only happening in production, so I don't want to switch on debugging.

+1  A: 

I took a good hard look and the only two things that come to mind that seem like the most likely are:

  • wrRequest could be null.
  • The ArrayList could have null values in it resulting in an enumerator that returns null values.

If it were me I would first focus my attention on this section. Is there any way possible that the Me.GetEnumerator will return an enumerator with one of the items having a null value?

For Each item As Referral In Me 
  item.Names ' Can item be null here causing the exception on the getter of Names?
Next item 
Brian Gideon
I'm trying to eliminate these possibilities. From the callstack, wrRequest is coming from Page.Request, which is hopefully never null. I've commented out the "Inherits ArrayList" line, and the only code that adds to the list is doing a New Referral() on the line beforehand. I'll keep looking. Planning to use ADPlus to dump the system the next time it's in this state, which will hopefully let me see if there's a null in the ArrayList (somehow).
Damien_The_Unbeliever
Well, finally managed to capture a memory dump when it's in the silly state, and the ArrayList does indeed have a null element in it (and more items than I expected), so it's time to go a hunting through more of that code.
Damien_The_Unbeliever
I am glad you found it!
Brian Gideon
A: 

As it turned out, there was a NULL in the arraylist - it turns out that, in certain circumstances, multiple threads were working on the same object (which inherits from arraylist) and calling Add(). So the null was appearing because the internal index was being incremented twice by different threads.

Damien_The_Unbeliever