It looks like you're interacting with the map before it has time to load. the map's load event fires after it has successfully loaded at least one layer (usually the base layer). When you create the map and add layers, you may want to move any other map actions, such as getting or setting extent, to a function that fires after the map's "load" event.
var map = new Map("mapdiv", {...});
var layer = new ArcGISDynamicMapServiceLayer(...);
map.addLayer(layer);
if (map.loaded) {
doSomething();
} else {
map.on("load", doSomething);
}