Creating custom widget

Topics: Customizing Orchard, General, Writing modules
May 22, 2012 at 6:14 PM

I added a widget to my custom module and it appears in the widgets section. However, when I try to add it to any layer, I get an exception "Creating widget failed: Object reference not set to an instance of an object."

How do I fix this issue?

May 22, 2012 at 6:16 PM

Very hard to say without more information, or the details on how you configured your widget.

May 22, 2012 at 6:27 PM

If it helps, I see this exception in App-Data\Logs - Orchard.Widgets.Controllers.AdminController - Creating widget failed: Object reference not set to an instance of an object.

What do you mean how I configured my widget? If you would like to see any particular piece of the code I can paste it here.


May 23, 2012 at 12:12 AM

If you run the site using the debugger, you should get a hint what's going on when the exception occurs. It may very well happen in a content part driver of your module.

May 23, 2012 at 12:33 AM
Edited May 23, 2012 at 12:52 AM

I ran it with the debugger on. The problem is that if I step through the code, it goes to n number of locations and I eventually give up and the page loads up giving the above notification- "Creating widget failed : Object reference not set to an instance of an object." 

I also tried putting breakpoints in Orchard.Widgets.Controllers.AdminController, but they don't get hit.

Any other clue?

May 23, 2012 at 12:39 AM
Edited May 23, 2012 at 12:40 AM

Aha. Please make sure that you have the following options checked in Visual Studio 2010 via Debug -> Exceptions (this may vary per VS configuration):

- Common Language Runtime Exceptions (Thrown + User-handled)
- Managed Debugging Assistants (Thrown + User-handled)

Having these options checked, the debugger should break at the exact line that causes the error.
Don't mind other exceptions, like views not being found for monitoring, assembly load failures etc. These are non critical. Just watch for any exception that occurs in your own module.

May 23, 2012 at 1:02 AM

Very weird. Still did the same thing. Didn't break anywhere, simply refreshed the page with error notification. I tried other pages and I could see lot of minor exceptions.

May 23, 2012 at 6:35 AM

You could hax the 'DefaultExceptionPolicy' class so that it logs more information about the exception (stacktrace etc...)

Jul 31, 2012 at 1:17 PM

The problem most likely is a missing CommonPart on your widget. I had the exact same errormessage and after debugging with intellitrace I discovered I didn't have a CommonPart, which the WidgetPart is asking for when you try to add the widget to a layer. Subtle, but very annoying.