Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 5 down vote accepted

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

share|improve this answer
Thanks! Now it works. – ALB Jan 18 '12 at 15:32
Great! You should mark the question as being answered. – kenbuja Jan 18 '12 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. – Kirk Kuykendall Jan 18 '12 at 20:33
Thanks again to all! – ALB Jan 19 '12 at 8:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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