NullReferenceException in Initialize

Add-in Express™ Support Service
That's what is more important than anything else

NullReferenceException in Initialize
 
briannesbitt


Guest


We have a client that just installed our Addin on 2 new computers (Win 7, Office 2013). The following error occurred on both machines. We are trying to work with them to test a 3rd machine (with un-obfuscated code) while we are there, but I thought I would see if you guys have any ideas.

After a reboot they said they saw an error dialog flash (didn't have PowerPoint running). Then on the next PowerPoint restart it gives the error "PowerPoint couldn't start last time. Safe mode...". Once you say no, PowerPoint starts up normally and all seems fine.

We have a log output at the end of AddinInitialize, AddinStartupComplete and OnRibbonLoaded. We have a TRACE log setting but this wasn't turned on for these executions. Our typical startup looks like this:

[26/06/2015 12:12:39 PM] [INFO] - - - - - - - - - - - - - - - - -
[26/06/2015 12:12:39 PM] [INFO] Initialize complete
[26/06/2015 12:12:39 PM] [INFO] StartupComplete complete
[26/06/2015 12:12:39 PM] [INFO] OnRibbonLoaded complete

For the NullException run it looks like this:

[26/06/2015 1:32:54 PM] [INFO] - - - - - - - - - - - - - - - - -
[26/06/2015 1:32:54 PM] [ERROR] System.NullReferenceException: Object reference not set to an instance of an object.
at OurCompany.Namespace.AddinModule.gjsmGCyHJHMcyBKPvp7(Object )
at OurCompany.Namespace.AddinModule.pBYOHZKrV(Object , EventArgs )
[26/06/2015 1:32:54 PM] [INFO] StartupComplete complete

It never completed the Initialize method (we are using .NET Reactor to obfuscate our code). When I looked up in our obfuscation translation map I can see pBYOHZKrV is the AddinModule_AddinInitialize method and the method signature matches. Unfortunately "gjsmGCyHJHMcyBKPvp7" doesn't exist in the mapping file. That makes me think its an inherited method from the base class?!?

When I throw our DLL into dotPeek I can confirm that "pBYOHZKrV" exists and has a signature of "void pBYOHZKrV(Object , EventArgs )" which of course matches the AddinInitialize method. I can also see "gjsmGCyHJHMcyBKPvp7" with a method signature of "int gjsmGCyHJHMcyBKPvp7(Object )". The namespace in the log output indicates its from the AddinModule class but I don't have any methods with that signature in our Addin class. I also looked at your source code for the ADXAddinModule() and neither do you!

Any ideas what might be causing the null ref ex? I also noticed that "OnRibbonLoaded" never gets called? Is it possible to run PowerPoint in some weird pre-load state that would cause it to not have its full environment ?!?
Posted 28 Jun, 2015 10:31:07 Top
briannesbitt


Guest


For what its worth if you run the cmd "POWERPNT.exe /EMBEDDING" PowerPoint runs in the background but no UI ever shows up... and I see our Initialize complete and StartupComplete logs but no OnRibbonLoaded. I don't get any exception with this though.

Is there any other way PowerPoint might be ran in a corporate environment like this by the system?
Posted 28 Jun, 2015 23:04:39 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello Brian,

You can also start PP programmatically e.g. via new PowerPoint.Application(). Also check if turning other COM add-ins off helps. When obfuscating, don't obfuscate public things.

You can also output the stack trace.


Andrei Smolin
Add-in Express Team Leader
Posted 29 Jun, 2015 05:20:09 Top
briannesbitt


Guest


After creating a standalone app that started PowerPoint programmatically I was able to reproduce the error locally. Turns out the CurrentInstance was null. We determined that if the standalone app called to create a new PowerPoint instance, then immediately called pptApp.Quit() on it, the CurrentInstance would get set to null somewhere during the AddinInitialize function. If the standalone app simply did a Thread.Sleep(500) before calling pptApp.Quit() then all would be fine as our AddinInitialize would complete fine. Also, OnRibbonLoaded was never called as expected since PowerPoint was never shown, we would just see it briefly in the task manager.

The bigger question is what on the users computer caused this scenario.... that answer I don't have.

Seeing as we only support 2007+ (ribbon only apps) we moved our initialize code from AddinInitialize to OnRibbonLoaded() so its not called when PowerPoint is started programmatically. This resolved the issue as not if that standalone starts PowerPoint and then immediately calls pptApp.Quit() I don't get a crash happening.

Needless to say, this makes sense anyway, as we don't need to initialize our app if no one can click on our ribbon buttons because PowerPoint hasn't been shown.
Posted 29 Jun, 2015 22:46:29 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Brian,

I suppose the issue occurs when a PP presentation is embedded using Insert | Object | Create from File, the Link to file check box is set and the user updates that presentation at startup. Another scenario would be selecting a presentation in Windows Explorer with the preview pane visible.

I think CurrentInstance is set to null when PP being closed raises the AddinBeginShutdown/AddinFinalize event. This may occur while the AddinInitialize event is being processed.

In my practice I saw the above occurring because there was an async call (such as web call) in the AddinInitialize event or even in the InitializeComponent method; I remember in one of the cases the async call was made by a WebBrowser component contained on a pane.

I'm not sure if you can make use of the HostStartMode property that Add-in Express introduced in version 7.7. It allows you to find out if PP was started from the Start Menu, programmatically, or by an embedded presentation.


Andrei Smolin
Add-in Express Team Leader
Posted 30 Jun, 2015 03:19:18 Top
briannesbitt


Guest


I'll play with those other 2 methods and see if they also create the issue. They seem more likely to be something they had done and actually causing the issue on their machines.

Everything you mentioned above I experienced yesterday morning! The BeginShutdown running while the initialize was still running etc. Our current initialize takes about 420ms to execute (most of which is our hardware license check).

Which do you think is the better solution? Keep the initialize code in AddinInitialize and use the HostStartMode to exit early if it wasn't started by UI. OR assuming OnRibbonLoaded only runs after the ribbon is loaded (ie we can then assume it was started by UI) and gets run every time then we just move our initialize code to OnRibbonLoaded?

Thanks.
Posted 30 Jun, 2015 07:14:49 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Now I see. In AddinInitialize and OnRibbonBeforeCreate, if HostStartMode==ADXHostStartMode.hsmEmbeddedDocument, you can delete all components from the add-in module. Additionally, in the OnInitialize event of the task panes manager, you disable all items. See also https://www.add-in-express.com/creating-addins-blog/2012/01/20/load-office-addin-on-condition/.


Andrei Smolin
Add-in Express Team Leader
Posted 01 Jul, 2015 06:42:13 Top
briannesbitt


Guest


Hey Andrei,

There was one scenario I saw where that wouldn't work. I used the "Insert | Object | Create from File" as you suggested to add a PPT file to an xls doc. This started PowerPoint in the background with a start mode of hsmEmbeddedDocument. When you right-click on the object and click Edit, PowerPoint is then shown with full Ribbon UI but the start mode is still hsmEmbeddedDocument. In that scenario our ribbon buttons should be shown and functioning even when hsmEmbeddedDocument is set.

We ended up moving our initialize code to OnRibbonLoaded and set a general Initialized to true flag after it is complete. Then in other code entrances (ShapeSelected event, WindowActivated event, keyboard event, Timers etc) we check that flag and exit early if it didn't get initialized. This worked in all of our test cases.

Thanks a lot for the tips and thoughts along the way on this one!
Posted 02 Jul, 2015 13:16:41 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Great! You are welcome!


Andrei Smolin
Add-in Express Team Leader
Posted 06 Jul, 2015 06:49:20 Top