Error in Canvas | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Error in Canvas

Whenever I try to run a co using canvas, the compiler shows an error that says, TypeError: null is not an object (evaluating 'canvas.getContext'). I have tried this code using a different compiler and it worked there. Any idea why it says this? Anything helps. Thanks.

6th Feb 2018, 1:51 AM
Dashiell
Dashiell - avatar
5 Answers
6th Feb 2018, 8:34 PM
The Coding Sloth
The Coding Sloth - avatar
+ 7
The problem is probably that your page is not loaded when you get the canvas and context. Try: onload = function() { // get the canvas // do other stuff like the context } The important part is the onload, that makes sure this code gets executed only when the page is fully loaded.
6th Feb 2018, 6:42 AM
The Coding Sloth
The Coding Sloth - avatar
+ 4
post your code here so we can see the problem.
6th Feb 2018, 4:14 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 3
Thanks for the help! Here is the code: https://code.sololearn.com/Wh5eWmkME7lq/?ref=app
6th Feb 2018, 8:26 PM
Dashiell
Dashiell - avatar
+ 3
Thank you so much! I really appreciate it.
7th Feb 2018, 1:31 AM
Dashiell
Dashiell - avatar