nullreferenceexception was unhandled by user code

477
7
Jump to solution
11-17-2012 11:09 PM
HushamHassan
Occasional Contributor
Hi,
I am developing an extension similar to American community Survey Mapping, I am trying to populate Combo box with TOC layers, or by using open command in my form, but I am getting  nullreferenceexception was unhandled by user code error  near:

Dim num2 As Integer = (strArray.Length - 1)


Also I am getting many warning messages  like  "Variable 'variableName' is passed by reference before it has been assigned a value. A null reference exception could result at runtime".

Here is part of the code where I get the error.

Private Sub CmdOpenFeture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdOpenFeture.Click         Dim obj2 As IEnumGxObject         Dim dialog As IGxDialog = New GxDialogClass         Dim filters As IGxObjectFilterCollection = DirectCast(dialog, IGxObjectFilterCollection)         Dim filter5 As IGxObjectFilter = New GxFilterTablesAndFeatureClassesClass         Dim filter As IGxObjectFilter = New GxFilterPersonalGeodatabasesClass         Dim filter2 As IGxObjectFilter = New GxFilterPGDBFeatureClassesClass         Dim filter3 As IGxObjectFilter = New GxFilterPGDBFeatureDatasetsClass         Dim filter4 As IGxObjectFilter = New GxFilterPGDBTablesClass         filters.AddFilter(filter5, True)         filters.AddFilter(filter, False)         filters.AddFilter(filter3, False)         filters.AddFilter(filter4, False)         Dim dialog2 As IGxDialog = dialog         dialog2.AllowMultiSelect = False         dialog2.ButtonCaption = "Select"         dialog2.Title = "Select Input File"         Dim flag As Boolean = dialog2.DoModalOpen(0, obj2)         dialog2 = Nothing         If flag Then             Dim strArray As String()             Dim fullName As String = obj2.Next.FullName             CboOpenfeature.Text = fullName             InputDataCmd.readFieldFromFeature(fullName, (strArray))             CboFeaturefield.Items.Clear()             Dim num2 As Integer = (strArray.Length - 1)             Dim i As Integer = 0             Do While (i <= num2)                 CboFeaturefield.Items.Add(strArray(i))                 i += 1             Loop             TxtOutputPath.Text = (InputDataCmd.featureWorkspacePath & "\" & InputDataCmd.featureName & "_joined.shp")         End If      End Sub

I do appreciate your help
Thank you
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
Here's one way to do it.

   Private Sub readFieldFromFeature(ByVal path As String, ByRef inputarray As String())          Try             Dim factory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory = New ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory             Dim workspace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace = DirectCast(factory.OpenFromFile(System.IO.Path.GetDirectoryName(path), 0), ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)             Dim fields As ESRI.ArcGIS.Geodatabase.IFields = workspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(path)).Fields             Dim fieldcount As Integer = fields.FieldCount              ReDim inputarray(fieldcount - 1)              For i As Integer = 0 To fieldcount - 1                 inputarray(i) = fields.Field(i).Name             Next          Catch ex As Exception             MsgBox(ex.ToString)         End Try      End Sub

View solution in original post

0 Kudos
7 Replies
KenBuja
MVP Esteemed Contributor
You've initialized the array strArray, but does the commandInputDataCmd do anything with the array? You're not doing any checking to see if strArray has any elements in it.

        Dim strArray As String()

        If strArray Is Nothing Then
            MsgBox("Empty")
        Else
            Dim num2 As Integer = (strArray.Length - 1)
        End If
0 Kudos
HushamHassan
Occasional Contributor
Thank you  kenbuja,
yes I am not doing any check,  I can't figure it out. any how I attached  the entire code for InputDataCmd


Many Thanks
0 Kudos
AlexanderGray
Occasional Contributor III
First of all without exception handlers, it will be next to impossible to debug the code.  Any method called by external code (oncreate, onclick, event handlers...) should always have exception handlers.  Should be the first lines of code any programmer writes. 
Secondly the array is declared empty and then never populated.  Checking for is nothing will not work because the array being declared is not nothing, it is however of zero length.  So if you attempt to get element length -1, you are passing in -1 as an index which will cause an error.  You need to check if the length of the array.  The exception being raised, if handled, would tell you so, if indeed that is the problem.
0 Kudos
KenBuja
MVP Esteemed Contributor
I have to disagree with you, Alexander. If you try to get the length of an array that was initialized but nothing added to it, you'll get the error message "System.NullReferenceException: Object reference not set to an instance of an object."

        Dim strArray As String()

        Try
            Dim test As Integer = strArray.Length
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

0 Kudos
HushamHassan
Occasional Contributor
Thank you all for your help.

kenbuja  you are right,  I am getting "System.NullReferenceException,  but I am still can't figure it out,  srtArray  return empty.

Many Thanks.
0 Kudos
KenBuja
MVP Esteemed Contributor
Here's one way to do it.

   Private Sub readFieldFromFeature(ByVal path As String, ByRef inputarray As String())          Try             Dim factory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory = New ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory             Dim workspace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace = DirectCast(factory.OpenFromFile(System.IO.Path.GetDirectoryName(path), 0), ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)             Dim fields As ESRI.ArcGIS.Geodatabase.IFields = workspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(path)).Fields             Dim fieldcount As Integer = fields.FieldCount              ReDim inputarray(fieldcount - 1)              For i As Integer = 0 To fieldcount - 1                 inputarray(i) = fields.Field(i).Name             Next          Catch ex As Exception             MsgBox(ex.ToString)         End Try      End Sub
0 Kudos
HushamHassan
Occasional Contributor
kenbuja,
Thank you so much,  that was very helpful, I manged to solve the problem,  the strArray was empty as you said, 
Thank you
0 Kudos