TypeError: Cannot read property 'getContext' of Null in JS

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

TypeError: Cannot read property 'getContext' of Null in JS #

The "Cannot read property 'getContext' of null" error occurs for 2 reasons:

  1. Calling the getContext method on a non-existent canvas element.
  2. Inserting the JS script tag above the HTML where the canvas element is declared.

cannot read property getcontext of null

Here is an example of how the error occurs.

index.js
const canvas = document.getElementById('does-not-exist'); console.log(canvas); // 👉️ null // ⛔️ Uncaught TypeError: Cannot read properties of null (reading 'getContext') const ctx = canvas.getContext('2d');

To solve the "Cannot read property 'getContext' of null" error, make sure you're providing the correct id when trying to get the canvas element. The error often occurs when providing an invalid id to the getElementById method.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <!-- ✅ id should match your JS code --> <canvas id="canvas" width="500" height="500"></canvas> <script src="index.js"></script> </body> </html>

To solve the "Cannot read property 'getContext' of null" error, place the JS script tag at the bottom of the body, after the canvas element has been declared.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <!-- ❌ BAD - script is run before canvas is created ❌ --> <script src="index.js"></script> <canvas id="canvas" width="500" height="500"></canvas> </body> </html>

The index.js script is run before the canvas element is created, so you can't access the element inside the index.js file.

index.js
const canvas = document.getElementById('canvas'); console.log(canvas); // 👉️ null // ⛔️ Cannot read properties of null (reading 'getContext') const ctx = canvas.getContext('2d');

You should place the JS script tag at the bottom of the body, after the DOM elements have been declared.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <canvas id="canvas" width="500" height="500"></canvas> <!-- ✅ GOOD - canvas is already created ✅ --> <script src="index.js"></script> </body> </html>

Now you can access the canvas element inside of the index.js file.

index.js
const canvas = document.getElementById('canvas'); console.log(canvas); // 👉️ canvas#canvas // ✅ works const ctx = canvas.getContext('2d'); console.log(ctx); // 👉️ CanvasRenderingContext2D

Conclusion #

The "Cannot read property 'getContext' of null" error occurs when calling the getContext method on a null value.

To solve the error, make sure the JS script tag is loaded after the HTML is declared and the id of the element exists in the DOM.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.