I'm working on a general library to use in all my iParts. As part of that, I want to be able to log not only which iPart had an error, but which specific content item experienced a problem.
So the constructor for my library class looks like:
public MyLibrary(Asi.Business.ContentManagement.ContentItem ci)
{
string s = ci.ContentItemName;
...
}
and in MyPart.Display.ascx.cs, in OnInit() I do this:
...
RSM.MyLibrary m = new RSM.MyLibrary(this.ContentItem);
...
My Display page properly inherits from Asi.Business.ContentManagement.ContentItem. To be safe, I created the content item initially with nothing in it, saved and published. When I try to add my part to a content item, I get this exception (red text on the Edit Content panel where my iPart should be):
The selected web part encountered an error and could not be displayed. The error message was:
System.NullReferenceException: Object reference not set to an instance of an object.
at RSM.MyLibrary..ctor(ContentItem ci) in C:\Data\WC\NonClient\RSM_Common\MyLibrary.cs:line 177
at RSM.wvActivity.DisplayClass.OnInit(EventArgs e) in C:\Data\WC\NonClient\wvSuite\trunk\wvActivity\wvActivity.Display.ascx.cs:line 60
at System.Web.UI.Control.InitRecursive(Control namingContainer)
at System.Web.UI.Control.AddedControl(Control control, Int32 index)
at System.Web.UI.ControlCollection.Add(Control child)
at System.Web.UI.WebControls.WebParts.GenericWebPart.GenericWebPartControlCollection.AddGenericControl(Control control)
at System.Web.UI.WebControls.WebParts.GenericWebPart.CreateChildControls()
at Asi.Web.UI.WebControls.AsiWebPart.CreateChildControls()
What am I missing here? Here are some possibilities I've considered:
- Test for a null object and provide a default
- Bypass this part of the process when in Design mode (but then I can't check for errors while adding the iPart to a page)
- Wait until a later stage in the lifecycle to check ContentItem (which?)