Wednesday, March 05, 2008

SharePoint Web part error "Object reference not set to an instance of an object"

I was getting "Object reference not set to an instance of an object" error while previewing a web part in web part gallery area.
Found that web part's CreateChildControls function is called even when you are previewing. This function was throuwing an error as in the code accesssing value of current list item using SPContext.Current.ListItem.

Added the following code and it worked fine
base.CreateChildControls();
if (SPContext.Current.ListItem == null)
return;

0 comments: