Recently came across a really interesting bug in SharePoint 2010. It’s when you are trying to upload multiple files using the ActiveX control, where you can drag and drop files or select multiple files. I started receiving “Object reference not set to an instance of an object” exceptions from the STSUpld control. The usual Google, ahem, Bing check revealed nothing. Just that I was not alone having this problem (hence this post).

SNAGHTMLe5d8a

The strange thing is that this only appeared on some sites and on some computers. After some fiddling around I noticed that it actually only happened on sites where I used a custom master page. My initial thought was that this had something to do with invalid HTML or missing delegate controls - but everything was just fine (alright then, I actually found a missing end div tag, but that didn’t solve it).

So I pulled out the almighty Reflector tool and checked the STSUpld.UploadCtl.GetFormInformation method to see what could possibly go wrong there. By inspecting the code for possible situations where a NullReferenceException could be thrown I found a couple of places where this is not checked for (ouch!):

  • The control needs a hidden **

    input
    

    ** element with the ID destination This one was there, I did not touch the upload.aspx file - and you should not either!

  • This destination field must have a value This is populated by the code behind

  • The control needs an

    input
    

    element of the type

    button
    

    with an **

    AccessKey
    

    ** attribute set to “O” For the OK button (might be different in other languages than English)

  • The control needs an

    input
    

    element of the type

    button
    

    with an **

    AccessKey
    

    ** attribute set to “C” For the Cancel button (might be different in other languages than English)

  • **All

    input
    

    ** element of the type **

    button
    

    ** must have the **

    AccessKey
    

    ** value set to non empty The control iterates through all input elements looking for the AccessKey to find the OK and Cancel buttons. In my case this was the one playing games with me!

The final point above was the thing that caused the NullReferenceException in the multiple upload control. In this case I had a few input elements with the type button without any

AccessKey

attributes in my master page (let’s not go into why they are there…). Nevertheless once I added the AccessKey attribute with a non-empty value the upload worked like a charm. Note that setting

AccessKey=""

causes the same exception, you need to set a value for it for instance;

AccessKey="_"

.

What can we learn from this! You should always check for null! Can’t be said too many times. Calling ToString() on objects without checking for null is the eight deadly sin.

PS: I think this post is worthy of a KB article, Microsoft? At least as a patch in upcoming Office updates…