1
var point = new Point(-49.9, 39.3)
var symbol = new PictureMarkerSymbol('http://www.prww-weather.com/hurricane/icons/post-tropical-cyclone.png', 50, 50)
var graphic = new Graphic(point, symbol);
map.on("load", map.graphics.add(graphic));

These four lines of code use the ArcGIS API for JavaScript to add a point to the map. However, for some reason, the code doesn't work and I get this error in the console:

Uncaught TypeError: Cannot read property 'add' of null

Can anyone tell me why this is occurring and how to fix it?

3

Heer The map is not yet loaded , it will throw this error, because there is no graphics property yet ,

also your code is wrong : you have to surround your code inside the call back function as below :

map.on("load", function() {
    map.graphics.add(graphic);
});

Adding Fiddle

  • When I use the code you've provided, the error isn't thrown anymore, but the point still doesn't show up on the map – Connor C Jun 17 '17 at 21:00
  • it depend on the projection system , and spatial ref , it could be added , just show , console.log(map.graphics); adn see if it was added corectly – Springer F Jun 17 '17 at 23:14
  • just done a test ; seems work perfct !!! here is a fiddle – Springer F Jun 18 '17 at 11:05
  • The fiddle works fine, but if I change the basemap to dark-gray, the map doesn't work anymore. Why does it do that? Is it just a bug with that basemap? – Connor C Jun 18 '17 at 18:05
  • 1
    The "terrain" and "dark-gray" options were added at v3.12. ~ developers.arcgis.com/javascript/3/jsapi/… – Bjorn Svensson Jun 20 '17 at 19:00

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.