title: d3 - Uncaught TypeError: Cannot call method 'attr' of null
body: <p>Here is my code:</p>
  
  <pre><code>var svg = d3.select("body").append("svg");
  console.log(svg.attr);
  svg.attr("width", width + margin.left + margin.right);
  svg.attr("height", height + margin.top + margin.bottom);
  svg.append("g");
  svg.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  </code></pre>
  
  <p>The console.log(svg) outputs:</p>
  
  <pre><code>function (t,e){if(arguments.length&lt;2){if("string"==typeof t){var n=this.node();return t=ja.ns.qualify(t),t.local?n.getAttributeNS(t.space,t.local):n.getAttribute(t)}for(e in t)this.each(xe(e,t[e]));return this}return this.each(xe(t,e))} 
  </code></pre>
  
  <p>However when I try to call svg.attr method right after that, I get this in the console:</p>
  
  <pre><code>Uncaught TypeError: Cannot call method 'attr' of null
  </code></pre>
  
  <p>Can anybody explain this? Clearly svg is not null as I log it in console before trying to use it...</p>
  
  <p><img src="https://i.stack.imgur.com/lw1xL.png" alt="enter image description here"></p>