---UPDATE--- see below

I have a scaling raster tool that I'm attempting to migrate from ArcGIS 9.3.1 VBA to VB.NET (Visual Studio 2010). I have a BaseTool that is used to put two graphic points defining the left and right extent of the image that I want to scale. After the second point is added to the view a form opens to input the scaling parameters. My issues is when I go to click the scale command button (within the form) I get the "Null Reference Exception was unhandled by user code, Object reference not set to an instance of an object", error. The error occurs on this line:

pMap = m_pMxDoc.FocusMap    

I have my ESRI references imported into the project for both pMap and m_pMxDoc. It seems like the form is not hooked to the ArcMap document? I tried to hook the form on load via the code below, but I get the same error.

Private Sub frmScaling_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim hook As Object = Nothing
            m_application = hook
            m_pMxDoc = m_application.Document
End Sub

Any suggestions of what I'm missing would be greatly appreciated.



I created the docable window class and memebers. The On Create sub now is:

Public Sub OnCreate(ByVal hook As Object) Implements ESRI.ArcGIS.Framework.IDockableWindowDef.OnCreate

        m_application = hook
        m_pMxDoc = m_application.Document

End Sub

Now when I click the command button within my form to scale the image I get a LoaderLock error (Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang). The error is still on this line:

pMap = m_pMxDoc.FocusMap

Is this error present because my form is open while the scaling action is trying to take place? So, the hook is now on the member "On Create", do I also need to hook within the button click on the form?

Are there any other good VB.NET form/code examples within ArcMap that contain some sort of button that interacts with the ArcMap view? I have only found this one,


As long as you have

Dim hook As Object = Nothing

You will get a null refrence exception when you access it.

You may want to read up on how to create commands and tools in ArcMap. You will certainly want to handle the OnCreate event - since that is where you get the "hook" from.

  • Thanks for your feedback. I'm changing my form to a IDockableWindowDef. I did not have the On Create member before. – artwork21 Apr 18 '11 at 18:04

Usually the procedure is to show/open the form on the click event of a Button control. This is where you pass the hook of your IApplication to the form, and in the load event of your form or somewhere else, you will get the IMxDocument from the IApplication

In your case, you are setting hook equal to nothing first and then attempting to get the IMxDocument from it.

  • So, I have the hook on both the Base command and the dockableWindowDef "On Create" members. The click member of the Base command (which opens the for) does not like when I try to hook there. – artwork21 Apr 19 '11 at 11:48
  • you should not get any problems there..Also you have mentioned 'scaling action' in your question. Can you elaborate on that? – Devdatta Tengshe Apr 20 '11 at 3:58
  • @Davdatta Tengshe 7, clicking the button within the form will re-scale the image. Let’s say the image was 1000ft long, you can assign a new scale factor to it then click the button and the image will now be 3500ft long. This function is used for when you only have one good point to georeference. Do you know of any good VB.NET form/code examples within ArcMap that contain some sort of button that interacts with the ArcMap view? I only found this one, edndoc.esri.com/arcobjects/8.3/GettingStarted/… – artwork21 Apr 20 '11 at 11:43

I figured out a work around for my error. Following the ESRI developer kit for 9.3.1 sample below, I eliminated the dockable window and used a straight up windows form. From my BaseCommand I changed the application variable to “Public Shared” and then referenced that shared variable on my form cmdScaleMap_Click to hook the button on the form (see code below).

Public Shared pPointColl As IPointCollection ‘ public shared variable ScaleImaage.vb

Public Shared m_application As IApplication ‘ public shared variable frmScalinCommand.vb 

‘OnCreate Member from the frmScalingCommand 
Public Overrides Sub OnCreate(ByVal hook As Object)

        If Not (hook Is Nothing) Then
            If TypeOf (hook) Is IMxApplication Then
                m_mxApplication = CType(hook, IMxApplication)
            End If
        End If

        m_application = CType(m_mxApplication, ESRI.ArcGIS.Framework.IApplication)

    End Sub

Public Sub cmdScaleMap_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdScaleMap.Click

        Dim applicationshared As IApplication = frmScalingCommand.m_application
        Dim sharedpts As IPointCollection = ScaleImage.pPointColl

        Dim mxDocument As IMxDocument = TryCast(application.Document, IMxDocument)
        Dim activeView As IActiveView = mxDocument.ActiveView
        Dim map As IMap = activeView.FocusMap

        m_activeViewEvents = CType(map, IActiveViewEvents_Event)

C:\Program Files\ArcGIS\DeveloperKit\SamplesNET\Desktop\DesktopWiringEvents\ReadMe.htm

Thank you to all who responded!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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