Select to view content in your preferred language

map.graphics is null (and likely related 'this._gc.surface' is null )

6337
5
01-11-2011 07:38 AM
MartenLiebster
Deactivated User
I've been able to display a map just fine. However I am having problems adding a graphic to it.

I first tried to just to do a map.graphics.add() and the graphics object is null (see graphic-null.jpg).

So to work around that I tried to create a graphics layer and then add the graphic to that, but got an error about the _gc.surface being null when trying to add the layer.

The maps show up just fine when I don't try to add them. A breakpoint in the js shows that it's getting the homePoint values.

I've looked through various samples to see that my code is the same and I don't see a difference. But I'm sure I'm missing something, as those work and mine doesn't.

In ASP.MVC code:
<link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.1/js/dojo/dijit/themes/tundra/tundra.css" />
<script src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.1" type="text/javascript"></script>

<script type="text/javascript">
    var homePoint = new esri.geometry.Point(@Model.Data.HomeLon, @Model.Data.HomeLat);    
</script>


In my js file (kept seperate for SoC preference)

var hMap;

dojo.require("esri.map");
dojo.require("esri.layers.graphics");
dojo.addOnLoad(initMaps);

function initMaps() {
    homeExtent = new esri.geometry.Extent(homePoint.x - 0.5,
    homePoint.y - 0.5,
    homePoint.x + 0.5,
    homePoint.y + 0.5);
   
    hMap = new esri.Map("homeMap", { extent: homeExtent, logo: false });
    var basemapURL = "http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"
    var basemap = new esri.layers.ArcGISTiledMapServiceLayer(basemapURL);
    hMap.addLayer(basemap);
}


Thanks for the help!!
0 Kudos
5 Replies
Kathleen_Crombez
Frequent Contributor
I was getting the same Jscript runtime error: "this._gc._surface" is null or not an object when adding a new graphics layer to the map.

I found the solution here: http://forums.esri.com/Thread.asp?c=93&f=2396&t=284131&g=1

The solution was to not load the graphics layer until after the map is finished loading.

selLayer = new esri.layers.GraphicsLayer();

dojo.connect(map, "onLoad", function() {
   map.addLayer(selLayer));
});


Not sure if you are trying to add your graphics to a new graphics layer or the default graphics layer maps.graphics

but this solution worked for me.
0 Kudos
by Anonymous User
Not applicable
Original User: mliebster

Thanks for the reply.

I got it working doing just that! I just neglected to post the solution.
0 Kudos
Kathleen_Crombez
Frequent Contributor
wish you would have posted the solution... I've been battling this one for over a week! 🙂
0 Kudos
by Anonymous User
Not applicable
Original User: mliebster

wish you would have posted the solution... I've been battling this one for over a week! 🙂


Sorry 😞

My bad - I should have done so.

There weren't many views on the thread and it had no replies, so I assumed I was doing something so stupid and unique to myself.
0 Kudos
AndreasHoogeveen
Emerging Contributor
I was getting the same Jscript runtime error: "this._gc._surface" is null or not an object when adding a new graphics layer to the map.

I found the solution here: http://forums.esri.com/Thread.asp?c=93&f=2396&t=284131&g=1

The solution was to not load the graphics layer until after the map is finished loading.

selLayer = new esri.layers.GraphicsLayer();

dojo.connect(map, "onLoad", function() {
   map.addLayer(selLayer));
});


Not sure if you are trying to add your graphics to a new graphics layer or the default graphics layer maps.graphics

but this solution worked for me.


If you choose this solution, then you have to add your graphics to the selLayer, and not to the default map.graphics layer. Apart from that, it works fine.
0 Kudos