Testing my VB.NET 2010 app in ArcMap an exception occurs when ICommandBars is instantiated.

' zoom to selected
Dim mxApp As ESRI.ArcGIS.Framework.IApplication
Dim numUID As UID = New ESRI.ArcGIS.esriSystem.UID
numUID.Value = "{AB073B49-DE5E-11D1-AA80-00C04FA37860}"
Dim commandBars As ICommandBars = mxApp.Document.CommandBars   '>>> error here!!!!!!!!
Dim cmdItem As ICommandItem = CommandBars.Find(numUID, False, False)
If Not (cmdItem Is Nothing) Then
End If

Message: Exception first-chance type 'System.NullReferenceException' in LUCAS.dll.

P.S. ArcGIS 10 SP1 on Windows 7, VS2010.

1 Answer 1


You've declared mxApp, but you haven't actually set it to anything. If you're using an addin, you can set that with the line

mxApp = My.ArcMap.Application

  • Great! You should mark the question as being answered.
    – kenbuja
    Jan 18, 2012 at 16:09
  • +1 Also note accessing Document.Commandbars before the document has loaded (e.g. within IExtension.Startup) has been known to cause Arcmap to crash. Hopefully that is fixed, in which case it might now throw a null reference exception too. Jan 18, 2012 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.