TransWikia.com

ArcMap - ArcObjects IGeoFeatureLayer Renderer is giving a NullReferenceException

Geographic Information Systems Asked by Anna3anana on June 9, 2021

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:MyAppsTestPluginMyPlugin"

    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

    pMxDoc.ActiveView.Refresh()
    pMxDoc.UpdateContents()
End Sub

One Answer

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:scratchtest.lyr"
Dim pLayerFile As ILayerFile
pLayerFile = New LayerFile
pLayerFile.Open(sPath)
Dim pGFL As IGeoFeatureLayer
pGFL = pLayerFile.Layer
Dim pURend As IUniqueValueRenderer
pURend = pGFL.Renderer
Console.WriteLine(pURend.ValueCount)

Answered by Hornbydd on June 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP