Refactoring a VBA add-in into VB.NET add-in.

I'm getting:

An unhandled exception of type ' System.NullReferenceException' occured in MyAddin.dll

Additional information: Object reference not set to an instance of an object.

I have a class that is calling three methods and it's failing on the first method because of the error I'm facing.

        If TypeOf pGFLayer.Renderer Is IUniqueValueRenderer Then
           Call LoadDefaultLegend(pMxDoc, pGFLayer, strSelectedLayer)

           Call RemoveJoin(pMxDoc.FocusMap, pLayer)
           pFLayer.DisplayField = "ACAD_layer"

           Call SelectLayerByName(strSelectedLayer, pMxDoc)
           Exit Sub
        End If

Then in the method it's trying to pass a parameter, but that parameter is giving me the unhandlded exception error (pRend = pGeoLayer.Renderer). The location of the .lyr file is fine.

Any ideas?

Sub LoadDefaultLegend(ByRef pMxDoc As IMxDocument, ByRef pGFLayer As IGeoFeatureLayer, strSelectedLayer As String)
    gstrHomeDir = "C:\MyApps\TestPlugin\MyPlugin\"

    Dim pGxLayer As IGxLayer
    Dim pGxFile As IGxFile

    pGxLayer = New GxLayer
    pGxFile = pGxLayer

    Dim strMxdPath As String
    Dim strLegendPath As String

    strLegendPath = gstrHomeDir & "\Legends\"
    ' Add correct lyr-file (01, 02, 03, ...)
    strLegendPath = strLegendPath & Right(strSelectedLayer, 2) & ".lyr"

    pGxFile.Path = strLegendPath
    Dim pGeoLayer As IGeoFeatureLayer
    pGeoLayer = pGxLayer.Layer

    'Get the Symbology from the layer
    Dim pRend As IFeatureRenderer
    pRend = pGeoLayer.Renderer

    pGFLayer.Renderer = pRend

End Sub
  • I would guess you would need to open the IGxFile after setting the path. I think you want the IGxFile.Open method, but I'm not 100% sure. I doubt simply setting the path to the layer file is sufficient to open it. Alternatively you could use the ILayerFile open the your layer file: resources.arcgis.com/en/help/arcobjects-net/componenthelp/… – danielm Sep 16 '19 at 14:40

I agree with @danielm ILayerFile is a more appropriate interface for interacting with a Layer File. Below is a code snippet to get you going:

Dim sPath As String
sPath = "C:\scratch\test.lyr"
Dim pLayerFile As ILayerFile
pLayerFile = New LayerFile
Dim pGFL As IGeoFeatureLayer
pGFL = pLayerFile.Layer
Dim pURend As IUniqueValueRenderer
pURend = pGFL.Renderer

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.