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
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
  • What happens if you change pArray = pIdentify.Identify(pPoint) to pArray = pIdentify.Identify(pGeom)? – fatih_dur Jul 3 at 2:10
  • pArray picks it up and it returns "Nothing". – coder2020 Jul 4 at 4:47

After hours of research, I figured it out. I need to create an instance of Point and then, assign longitude variable (pLonX) to X and latitude variable (pLatY) to Y. It resolved my issue.

Dim pPoint As IPoint = New Point()

pPoint.X = pLonX
pPoint.Y = pLatY
pIdentify = CType(pFeatureLayer, IIdentify)
pArray = pIdentify.Identify(pPoint)
| improve this answer | |

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.