With a longitude and latitude value, I am trying to identify feature (shapefile) on feature layer with no mouse click or user interaction. On debugging mode with an attach process on ArcMap.exe, the code was executed successfully but on run time where I need to autorun a mxd report, the code failed at this lines:
pIdentify = CType(pFeatureLayer, IIdentify)
pArray = pIdentify.Identify(pPoint)
Error Message: Object reference not set to an instance of an object.
I'm using VB.NET. ArcMap Version is 10.2. Why does my code works on debugging mode and not on run time when it runs a mxd report automatically? Does identify feature on a layer works without a mouse click event? What am I missing?
Below is my code example:
Dim strShapeFilePath As String = "C:\FolderName\Shape_File.shp"
Dim pFileInfo As FileInfo = New FileInfo(strShapeFilePath)
Dim pWorkspaceFactory As IWorkspaceFactory
Dim pFeatureWorkspace As IFeatureWorkspace
Dim pFeatureLayer As IFeatureLayer
Dim pMxDocument As IMxDocument
Dim pMap As IMap
Dim pLatY As Double = 38.540611
Dim pLonX As Double = -121.489388
Dim pFeature As IFeature
Dim pFeatureClass As IFeatureClass
Dim pActiveView As IActiveView
Dim pIdentify As IIdentify
Dim pArray As IArray
Dim pPoint As IPoint
Dim pGeom As IGeometry
pWorkspaceFactory = new ShapefileWorkspaceFactoryClass()
pFeatureWorkspace = TryCast(pWorkspaceFactory.OpenFromFile(pFileInfo.Directory.ToString(), 0),IFeatureWorkspace)
pFeatureClass = pFeatureWorkspace.OpenFeatureClass(pFileInfo.Name)
pFeatureLayer = New FeatureLayer
pFeatureLayer.FeatureClass = pFeatureClass
pFeatureLayer.Name = pFileInfo.Name
pFeatureLayer.Visible = True
pMxDocument = Me.mxDocument
pMap = pMxDocument.FocusMap
pActiveView = mxDocument.ActiveView
pActiveView.Refresh()
pMap.AddLayer(pFeatureLayer)
pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(pLonX, pLatY)
pGeom = pPoint
pIdentify = CType(pFeatureLayer, IIdentify)
pArray = pIdentify.Identify(pPoint)
Dim intOID As Integer = -1
If pArray IsNot Nothing Then
Dim pObject As Object = pArray.Element(0)
Dim pFeatureIdentifyObj As IFeatureIdentifyObj = TryCast(pObject, IFeatureIdentifyObj)
Dim pRowIdentifyObject As IRowIdentifyObject = TryCast(pFeatureIdentifyObj, IRowIdentifyObject)
pFeature = TryCast(pRowIdentifyObject.Row, IFeature)
intOID = pFeature.OID
End If
pArray = pIdentify.Identify(pPoint)
topArray = pIdentify.Identify(pGeom)
?