Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I am trying to access a map layer, which I can connect to.

                    Log.Write("Connected.", "myclass");
                    if (agsconn.IsConnected)
    ESRI.ArcGIS.Server.IServerObjectManager som = agsconn.ServerObjectManager;
                            IServerContext serverContext = som.CreateServerContext(mapDocName, "MapServer");
                            IMapServer mapServer = serverContext.ServerObject as IMapServer;
                            Log.Write("Server context established MapName=" + mapServer.DefaultMapName, "myclass");
                            ESRI.ArcGIS.Carto.IMapServerObjects mapSO = mapServer as ESRI.ArcGIS.Carto.IMapServerObjects;
                            Log.Write("IMapServerObjects created", "myclass");
                            IMap map = mapSO.get_Map(mapServer.DefaultMapName);
                            Log.Write("Layers map established", "myclass");
                            //int layerIndex = -1;
                            IFeatureLayer fLayer = map.get_Layer(0) as IFeatureLayer;

The map was created with direct connect, I don't know if that makes a difference.

So far, the arcgis connects, and everything goes fine, but it stops here:

IMap map = mapSO.get_Map(mapServer.DefaultMapName);

I checked mapServer.DefaultMapName, and it returns "Layers" so that's all correct.

mapSO also works because mapServer and mapSO are the same object basically.

So how is there a null here?

My log prints out an exception and never reaches "layers map established".

The exception:

Object reference not set to an instance of an object.

I've tried a few other maps too and it doesn't work. But this code is straight from sample code I've seen on ESRI website, and it works for ESRI.

I'm using ArcGIS 9.3.1.

share|improve this question
Is the map service based on a MSD file? MSD-based services do not have access to fine-grained ArcObjects.. – Petr Krebs Mar 19 '12 at 20:06
Yes I was just reading about that just now. You are right I believe. I am gonna try to see if MXD files fix this problem. – Dexter Mar 19 '12 at 20:20
up vote 0 down vote accepted

Solved. you have to make sure your Map Service is an MXD file, not an MSD. Otherwise you cannot use "get_Map" or "get_Layer" etc. in geodatabase API.

Also enable web services in the properties of ArcGIS Manager.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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