This may cause because you are trying to get canvas object before loading the DOM elements.
Solution is call a method onload of body and init canvas inside that funtcion.
<body onload="init()">
function init() {
canvas = document.getElementById("gameCanvas");;
context = canvas.getContext('2d');
}
Hope this helps you
Happy Coding :)