1

I'm doing a simple test of a tool that uses a dockable window in an ArcGIS Desktop Add-In. I have a button that opens the window which will be the form that interacts with my other classes. I'm trying to access the maps and layers of an mxd but when debugging I get a NullReferenceException saying my layer object is not set to an instance of an object. Anyone know why this is happening?

namespace DockableExportTopoTool
{
    public class LaunchButton : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        protected override void OnClick()
        {
            UID dockableWinUID = new UID();
            dockableWinUID.Value = ThisAddIn.IDs.DockableWindow1;
            IDockableWindow dockableWindow1 = ArcMap.DockableWindowManager.GetDockableWindow(dockableWinUID);
            dockableWindow1.Show(true);
        }
    }
    public partial class DockableWindow1 : UserControl
    {
        Site site;
        public DockableWindow1(object hook)
        {
            InitializeComponent();
            this.Hook = hook;
        }
        private void btnExport_Click(object sender, EventArgs e) 
        {
            site = new Site();
        }
    }
    class Site
    {
        private IMxDocument mxdoc;
        private const string quadsLayerFilePath = @"G:\USGS 24k Map Sheets.lyr";
        public Site() 
        {
            mxdoc = ArcMap.Application.Document as IMxDocument;
            map = mxdoc.FocusMap;
            SetLayerReferences();
        }
        private void SetLayerReferences()
        {
            for (int i = 0; i < map.LayerCount; i++)
            {
                IGxFile gxFile = map.Layer[i] as IGxFile;
                if (gxFile.Path == quadsLayerFilePath)
                {
                    MessageBox.Show(gxFile.Path);
                    break;
                }
            }
        }
    }
}

The gxFile object is raising the exception, right now I'm just displaying a MessageBox to show whether I've accessed the layer I need.

ArcGIS 10.1 SP1 C#.NET 4.0

2
  • 2
    Why are you using IGxFile? Seems like IFeatureLayer would make more sense. That way you could cast IFeatureLayer.Featureclass to IDataset and figure out the path from there. Commented Sep 13, 2016 at 16:32
  • Just a quick way to get the file name for now I guess, I tried using IFeatureLayer but I'm getting the same NullReferenceException that I was getting for IGxFile. The point is I don't understand why I'm getting that error. Commented Sep 13, 2016 at 16:43

1 Answer 1

2

If you look at the api help file for the IGxFile interface it shows the classes that implement the interface. Layer returned by your map object is not a class that can implement the interface IGxFile.

The clue is the "Gx" bit, interfaces that have a "Gx" in their name tend to be interfaces for navigating file systems/workspaces, primarily within ArcCatalog or the browser panel in ArcMap.

So you are pointing gxFile to a class that cannot use it. As @KirkKuykendall points out you should be doing it via IFeatureLayer as you are obtaining Layers from the map not datasets from their source which IGxFile is expecting.

So in VB you would use something like

dim pFL as IFeatureLayer
pFL = map.layer(i) 'QI ILayer into IFeatureLayer
dim pDS as IDataset
pDS = pFL.FeatureClass

' Assumes dataset is a geodatabase featureclass
debug.print pDS.Workspace.PathName & "\" & pDS.Name 
0

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.