DEV Community

Discussion on: Pure and Simple - Tic Tac Toe with Javascript

Collapse
 
starrysamurai profile image
StarrySamurai

I keep recieving this error- Uncaught TypeError: Cannot set property 'innerHTML' of null. Anyone know how to fix this?

Collapse
 
bornasepic profile image
Borna Šepić

Hey, sorry to hear your having trouble with this tutorial. Could you post your code on codesandbox.io/ or some other similar platform? I'd be more than happy to take a look and help you out

Collapse
 
justkundai profile image
Just-Kundai

Actually having the same error message. Did you guys resolve this?

Thread Thread
 
truepiotrek profile image
truepiotrek

Can you post your repo/code?

Collapse
 
linehammer profile image
linehammer

In JavaScript almost everything is an object, null and undefined are exception. if a variable has been declared, but has not been assigned a value, is automatically assigned the value undefined . Therefore, if you try to access the value of such variable, it will throw Uncaught TypeError cannot set property ‘0’ of undefined/null .

JavaScript null and undefined is one of the main reasons to produce a runtime errors . This happens because you don’t check the value of unknown return variables before using it. If you are not sure a variable that will always have some value, the best practice is to check the value of variables for null or undefined before using them. The standard way to catch null and undefined simultaneously is this:

if (variable == null) {
// your code here.
}

Because null == undefined is true, the above code will catch both null and undefined.

Also you can write equivalent to more explicit but less concise:

if (variable === undefined variable === null) {
// your code here.
}

This should work for any variable that is either undeclared or declared and explicitly set to null or undefined.

Collapse
 
pcamellon profile image
Pcamellon

I think you’re trying to access an undeclared variable. Look for typos!