Select to view content in your preferred language

question about null reference in VB2010

1204
9
06-28-2011 04:57 PM
jihuawang
Emerging Contributor
Hello

I am upgrading a stand-alone window form application from VB2005 to VB2010.
I have a quick question when I compiled a window form application using vb.net 2010.
When I load the form, at the line of
pMap = m_pMxDoc.FocusMap

An error message said

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
 
I have defined in the class already
Private m_pApp As IApplication
Private m_pMxDoc As IMxDocument
Have you ever met this problem?

Does anyone see any sample GIS code in VB2010? I didn't see much sample scripts in VB2010 I can follow.

http://arcscripts.esri.com/

Thanks a lot for your help!
0 Kudos
9 Replies
RichardWatson
Deactivated User
Where did you set m_pMxDoc?  By default reference variables are null unless you explicitly set them.
0 Kudos
jihuawang
Emerging Contributor
Where did you set m_pMxDoc?  By default reference variables are null unless you explicitly set them.


I set m_pMxDoc in the class
Public Class form1
    Inherits System.Windows.Forms.Form
    Private m_pApp As IApplication
    Private m_pMxDoc As IMxDocument

    Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        pMap = m_pMxDoc.FocusMap

Is there any problem with it?

This is a standdard-alone application. Users will install .exe file and then add the icons from customize and add from command in ArcMap.
I think m_pMxDoc will not be initialized until the form is loaded and some GIS maps are selected.
Is my understanding correct?

Thanks a ot!
0 Kudos
RichardWatson
Deactivated User
I still do not see where you set m_pMxDoc.  The statement "Private m_pMxDoc as IMxDocument" does not set the value of m_pMxDoc.  All this statement does is to declare a variable named m_pMxDoc to be of type IMxDocument.

I also think that IApplication is all about ArcMap.  Are you running inside ArcMap?  Is this an external application which is connecting to ArcMap via the ROT?  Where are you getting IApplication, i.e. what specific line of code is setting m_pApp?
0 Kudos
jihuawang
Emerging Contributor
I still do not see where you set m_pMxDoc.  The statement "Private m_pMxDoc as IMxDocument" does not set the value of m_pMxDoc.  All this statement does is to declare a variable named m_pMxDoc to be of type IMxDocument.

I also think that IApplication is all about ArcMap.  Are you running inside ArcMap?  Is this an external application which is connecting to ArcMap via the ROT?  Where are you getting IApplication, i.e. what specific line of code is setting m_pApp?


Thanks a lot for your prompt reply!
Yes, I run it inside ArcMap. 
I set m_pApp in the class of form1

    Public Sub New(ByVal pApp As IApplication)
        MyBase.New()
        InitializeComponent()
        m_pMxDoc = CType(m_pApp.Document, IMxDocument)
    End Sub
%%%
In the command class of form1
    Public Overrides Sub OnCreate(ByVal hook As Object)
        If TypeOf hook Is IMxApplication Then
            m_pApp = CType(hook, IApplication)
        End If
    End Sub

    Public Overrides Sub OnClick()
        Dim winform As form1 = New form1(m_pApp) 
        winform.ShowDialog()
    End Sub


Any problem with the above lines?

Thank you very much!
0 Kudos
RichardWatson
Deactivated User
It isn't really clear to me what you have.

Obviously m_pMxDoc is null so the question is why.

Just put breakpoints in each function and look at the values of m_pApp and m_pMxDoc as the code executes.  Maybe the flow isn't what you think it is.
0 Kudos
jihuawang
Emerging Contributor
Thanks a lot for your reply! Yes I have set a breakpoint and add  m_pApp and m_pMxDoc in watch list. They are null if I run the code from VB2010.
But generally the users will run a compiled executable file and then add it as a command in ArcMap. Then m_pApp is the current arcmap.
Can you help me to take a look at my codes?
I have attached the complete code. You may unzip it and rebuild the PRO2010 in VS 2010. I also include a word document showing how to run the older version of it for arcgis 9.3.
Basically I am upgrading my old code from VB.net 2005 to VB.net 2010 to make it compatible with GIS desktop 10. I have modified my codes according to
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/How_to_migrate_ArcGIS_9...

A possible error I made may be the start form I specified. In the old version, I didn't need to specify any start form.
Thanks a lot!
0 Kudos
AlexanderGray
Honored Contributor
If your code is meant to run from ArcMap and you try to start the form directly from visual studio (debug?), it will not work.  You need to pass in a valid reference when you create the form, VS cannot do this.  I suggest you change the debug properties of your start up project to start arcmap, then invoke the form through the command and check the values being passed in.  You can still set break points, step through the code, view values and edit the code when you start ArcMap in debug mode.
0 Kudos
jihuawang
Emerging Contributor
If your code is meant to run from ArcMap and you try to start the form directly from visual studio (debug?), it will not work.  You need to pass in a valid reference when you create the form, VS cannot do this.  I suggest you change the debug properties of your start up project to start arcmap, then invoke the form through the command and check the values being passed in.  You can still set break points, step through the code, view values and edit the code when you start ArcMap in debug mode.


Thanks a lot for your help!
I changed the start page and now start from arcmap.exe.
My generated tool is supposed to installed and then added from customize-command. Without installation, arcmap doesn't include my generated tool and I cannot debug. Any good solution?

Actually I think my upgrade should not be so difficult. The old code works great in ArcGIS 9.3 and all the tools have passed debugging. The publication is here
http://www.esri.com/news/arcuser/0408/groundwater.html
Any clue about the upgration?

Thanks a lot!

Jihua
0 Kudos
AlexanderGray
Honored Contributor
Jihua,
Component registration has changed at 10.  There are a few articles in the help about migrating your code to arcgis 10.  If you go to customize and add from file, can you select the tlb with the dll and load the command that way?  If so your command is fine, you just need to update the registration.
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Migrating_ArcGIS_9_3_De...
0 Kudos