Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Javier Lim
476 Pointsgame.js:2 Uncaught TypeError: Cannot read property 'getContext' of null => why do i get this error
this is the html code:
<!DOCTYPE html> <html> <head> <title>Shooter Game</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <canvas id="canvas" width="640" height="480"></canvas> <script src="game.js"></script> </body>
And this is the Javascript code: const canvas = document.getElementById("shooter"); const context = canvas.getContext("2d");
2 Answers

Steven Parker
242,770 PointsYou don't have any HTML elements with the ID of "shooter", so the "getElementById" is returning null.
Did you mean to say "canvas" instead? Or give your canvas element an ID of "shooter"?

Javier Lim
476 Pointsso what is the code?

Steven Parker
242,770 PointsIt depends on which code you want to change: if you change the HTML, give the canvas id="shooter"
(instead of id="canvas"
). Or you could change the JavaScript and use getElementById("shooter")
instead of getElementById("canvas")
.