Why do I get an error. Can't read value of null on getContext? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why do I get an error. Can't read value of null on getContext?

var canvas = document.getElementById("canvas"); var context = canvas.getContext("2d"); var x = 300; var y = 350; context.arc(x, y, 50, 0, 2 * Math.PI); context.fillStyle="red"; context.fill();

13th Apr 2022, 2:18 PM
Nate Washburn
2 Answers
+ 1
Your script loaded before the document is ready. Just wrap your whole Javascript code in - window.onload = () => { ... }
13th Apr 2022, 3:06 PM
Arnav Gumber
Arnav Gumber - avatar
+ 3
Perhaps this post can help understanding https://www.sololearn.com/post/90825/?ref=app
13th Apr 2022, 2:24 PM
Ipang