Im getting this error:
Uncaught TypeError: Cannot read property 'getContext' of null
in my demo.js page
I have included demo.js page in my html page (start page) .
How can i solve this error?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Manoj BhoirRank #599 · 4K reputationPosted May 3, 2019, 11:52 PM EDT
I guess the problem is your js runs before the html is loaded.
If you are using jquery, you can use the document ready function to wrap your code:
Afzaal Ahmad ZeeshanRank #60 · 27.6K reputationPosted May 3, 2019, 10:45 AM EDT
My guess is that you are trying to use the HTML5 canvas element to render something on top of it, in that case the variable that is holding the reference of your canvas is null. Make sure that the canvas element (or whatever element that has this getContext() function) exists in the DOM.
Bryian TanRank #396 · 6.3K reputationPosted May 3, 2019, 11:27 AM EDT
Laxmidhar SahooRank #383 · 6.6K reputationPosted May 4, 2019, 12:09 AM EDT
$(function() { var Grid = function() { var canvas = document.getElementById("canvas"); if(canvas.getContext) { var context = canvas.getContext("2d"); } } });