0

I am trying to update a field value according to the selected OBJECTID. But get System.NullReferenceException error. Don't know what's wrong with the code.

My code is as follows:

        pActiveView = pMxDoc.FocusMap
        pMxDoc = My.ArcMap.Application.Document
        pMap = pMxDoc.FocusMap

        Dim layerNum = GetIndexNumberFromLayerName(pActiveView, "dev_db.DBO.Segment")

        Dim pFLayer As IFeatureLayer
        pFLayer = pMap.Layer(layerNum)

        Dim featureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass = pFLayer.FeatureClass

        Dim feature As IFeature = pFLayer.GetFeature(1)
        Dim nameFieldIndex As Integer = pFLayer.FindField("Seg_ID")
        feature.Value(nameFieldIndex) = "12/11"
        feature.Store()

The method is called in an editing session, and confirm has the OBJECTID 1 in the specified layer.

I am using arcobjects 10.2.2 and vb.net arcmap add-in.

2 Answers 2

2

GetFeature() is a method of IFeatureClass, not IFeatureLayer.

Change this:

Dim feature as IFeature = pFLayer.GetFeature(1)

to this:

Dim feature as IFeature = pFLayer.FeatureClass.GetFeature(1)

Looks like you're creating a featureclass object but not using it.

1
  • Hi Icoursey, a strange thing when I am in editing session and try to update value in attribute table, when I perform feature.store and open the attribute table, all values disappear. When I exit editing session, the records return, but the value not updated. This drive me crazy, any advice on it? Thanks a lot!
    – ppLily
    Oct 21, 2014 at 1:04
1

I always recommend to check for null values when you are geting features from feature classes, feature class from feature layers, etc. That will prevent you this type of errors, and in the future will be easier to find bugs.

Dim pFLayer As IFeatureLayer
pFLayer = pMap.Layer(layerNum)

Dim featureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass = pFLayer.FeatureClass
If featureClass = DbNull.value then
    Return
End If

Dim feature As IFeature = featureClass.GetFeature(1)
If feature = DbNull.value then
    Return
End If

Dim nameFieldIndex As Integer = pFLayer.FindField("Seg_ID")
If nameFieldIndex = -1 then
    Return
End If

feature.Value(nameFieldIndex) = "12/11"
feature.Store()

With that code you had recognized that the feature was null, and realized that the method "GetFeature()" from the feature layer was wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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