getContext() returns null | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

getContext() returns null

I'll really appreciate some help here. Why does this code return uncaught type error cannot 'getContext'of null when I run it on PC? Also, when I use external script here, it says the same thing I checked the web but the solutions I got didn't work. https://code.sololearn.com/WmRGdHv2wxp6/?ref=app

16th Oct 2018, 12:28 PM
Umeozulu Tochukwu
Umeozulu Tochukwu - avatar
10 Answers
+ 1
Basil About error putting script in js tab, is normal because the js tab content is inserted in "final" html head tag and because here, canvas element is not yet loaded, your code CANNOT get reference to it then getContext cannot be called on a null object. Anyway i tried your code on Firefox (PC) and all works (i executed it on Code Playground web version)
16th Oct 2018, 12:57 PM
KrOW
KrOW - avatar
+ 2
Thanks for the help, KrOW. It's now working properly on my browser.
16th Oct 2018, 1:21 PM
Umeozulu Tochukwu
Umeozulu Tochukwu - avatar
+ 2
I used the <script> tag to link my script code on the last line in the <body> tag while the actual script was in the JS tab here on Code playground but I still get the error. Hope you don't mind checking the code again.
16th Oct 2018, 1:46 PM
Umeozulu Tochukwu
Umeozulu Tochukwu - avatar
+ 2
Basil I explained why it happen... For understand better, run this code https://code.sololearn.com/WlA8Fx8bl6nI/?ref=app and FOLLOW execution with messages printed in console
16th Oct 2018, 2:20 PM
KrOW
KrOW - avatar
+ 2
Thanks KrOW.
16th Oct 2018, 7:32 PM
Umeozulu Tochukwu
Umeozulu Tochukwu - avatar
+ 1
The error is not here but on my pc's chrome browser. Also, if you copy the code in the script tag to the JS tab in code playground, it shows error too.
16th Oct 2018, 12:54 PM
Umeozulu Tochukwu
Umeozulu Tochukwu - avatar
+ 1
Basil Just for curiosity, what it was the problem?
16th Oct 2018, 1:22 PM
KrOW
KrOW - avatar
+ 1
KrOW, I've been checking but haven't found anything. The first code(that wasn't working) is the same with the second code(current one). But when I run the first one it doesn't give the expected result, though it doesn't show error again. I'll let you know when I find anything.
16th Oct 2018, 1:43 PM
Umeozulu Tochukwu
Umeozulu Tochukwu - avatar
+ 1
You are welcome 👍👍👍
16th Oct 2018, 7:37 PM
KrOW
KrOW - avatar
0
I get no error... Can you tell where you run it (browser)? You run it on SL code playground or what?
16th Oct 2018, 12:44 PM
KrOW
KrOW - avatar