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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Oct 16, 2014 at 3:15

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.

answered Oct 16, 2014 at 14:06
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! Commented 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.

answered Oct 17, 2014 at 7:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.