Uncaught Typeerror: Cannot Read Property 'click' of Null on Keydown
JavaScript Errors and How to Fix Them
JavaScript tin can be a nightmare to debug: Some errors it gives can exist very hard to understand at beginning, and the line numbers given aren't always helpful either. Wouldn't it be useful to have a list where you could expect to notice out what they mean and how to prepare them? Hither you become!
Below is a list of the foreign errors in JavaScript. Different browsers can requite you different letters for the same error, and so there are several unlike examples where applicable.
How to read errors?
Before the list, let'south quickly look at the structure of an error message. Agreement the construction helps understand the errors, and you'll accept less trouble if you run into any errors not listed here.
A typical error from Chrome looks similar this:
Uncaught TypeError: undefined is non a function
The structure of the error is every bit follows:
- Uncaught TypeError: This part of the message is usually not very useful. Uncaught ways the mistake was not caught in a
catch
statement, andTypeError
is the mistake's name. - undefined is not a function: This is the message role. With mistake messages, you lot have to read them very literally. For example in this instance it literally means that the code attempted to use
undefined
like information technology was a function.
Other webkit-based browsers, like Safari, give errors in a similar format to Chrome. Errors from Firefox are like, just do not always include the first part, and recent versions of Internet Explorer also give simpler errors than Chrome – but in this case, simpler does non always mean better.
At present onto the actual errors.
Uncaught TypeError: undefined is not a function
Related errors: number is non a part, object is not a role, string is non a function, Unhandled Error: 'foo' is not a part, Office Expected
Occurs when attempting to call a value similar a part, where the value is not a office. For example:
var foo = undefined; foo();
This fault typically occurs if you are trying to phone call a office in an object, but y'all typed the name wrong.
var x = certificate.getElementByID('foo');
Since object backdrop that don't exist are undefined
by default, the above would result in this mistake.
The other variations such every bit "number is non a function" occur when attempting to call a number similar it was a function.
How to ready this error: Ensure the function proper noun is correct. With this mistake, the line number volition commonly point at the correct location.
Uncaught ReferenceError: Invalid left-paw side in assignment
Related errors: Uncaught exception: ReferenceError: Cannot assign to 'functionCall()', Uncaught exception: ReferenceError: Cannot assign to 'this'
Caused by attempting to assign a value to something that cannot be assigned to.
The almost common example of this mistake is with if-clauses:
if(doSomething() = 'somevalue')
In this example, the programmer accidentally used a single equals instead of two. The bulletin "left-manus side in assignment" is referring to the part on the left side of the equals sign, and then similar y'all can encounter in the above example, the left-paw side contains something you tin can't assign to, leading to the error.
How to fix this fault: Make certain y'all're not attempting to assign values to office results or to the this
keyword.
Uncaught TypeError: Converting round construction to JSON
Related errors: Uncaught exception: TypeError: JSON.stringify: Not an acyclic Object, TypeError: cyclic object value, Circular reference in value statement not supported
Always caused past a circular reference in an object, which is then passed into JSON.stringify
.
var a = { }; var b = { a: a }; a.b = b; JSON.stringify(a);
Because both a
and b
in the above example have a reference to each other, the resulting object cannot be converted into JSON.
How to set this mistake: Remove circular references similar in the example from any objects you want to convert into JSON.
Unexpected token ;
Related errors: Expected ), missing ) subsequently argument list
The JavaScript interpreter expected something, only information technology wasn't at that place. Typically caused by mismatched parentheses or brackets.
The token in this error tin vary – information technology might say "Unexpected token ]" or "Expected {" etc.
How to fix this fault: Sometimes the line number with this error doesn't point to the correct place, making it difficult to set.
- An error with [ ] { } ( ) is normally acquired by a mismatching pair. Check that all your parentheses and brackets have a matching pair. In this case, line number will often point to something else than the problem grapheme
- Unexpected / is related to regular expressions. The line number for this volition usually be correct.
- Unexpected ; is usually caused by having a ; inside an object or array literal, or within the argument list of a part call. The line number volition usually be correct for this case every bit well
Uncaught SyntaxError: Unexpected token ILLEGAL
Related errors: Unterminated String Literal, Invalid Line Terminator
A string literal is missing the closing quote.
How to fix this error: Ensure all strings take the correct closing quote.
Uncaught TypeError: Cannot read property 'foo' of zero, Uncaught TypeError: Cannot read property 'foo' of undefined
Related errors: TypeError: someVal is null, Unable to get property 'foo' of undefined or null reference
Attempting to read null
or undefined
as if information technology was an object. For case:
var someVal = null; panel.log(someVal.foo);
How to fix this fault: Ordinarily caused by typos. Check that the variables used most the line number pointed by the error are correctly named.
Uncaught TypeError: Cannot prepare property 'foo' of null, Uncaught TypeError: Cannot ready property 'foo' of undefined
Related errors: TypeError: someVal is undefined, Unable to set property 'foo' of undefined or null reference
Attempting to write null
or undefined
every bit if it was an object. For example:
var someVal = null; someVal.foo = 1;
How to set up this error: This likewise is usually caused by typos. Cheque the variable names near the line the error points to.
Uncaught RangeError: Maximum call stack size exceeded
Related errors: Uncaught exception: RangeError: Maximum recursion depth exceeded, as well much recursion, Stack overflow
Usually caused by a bug in program logic, causing infinite recursive function calls.
How to fix this mistake: Check recursive functions for bugs that could cause them to continue recursing forever.
Uncaught URIError: URI malformed
Related errors: URIError: malformed URI sequence
Acquired by an invalid decodeURIComponent call.
How to gear up this error: Check that the decodeURIComponent
call at the error'south line number gets correct input.
XMLHttpRequest cannot load http://some/url/. No 'Admission-Control-Allow-Origin' header is present on the requested resource
Related errors: Cross-Origin Request Blocked: The Aforementioned Origin Policy disallows reading the remote resource at http://some/url/
This error is always caused by the usage of XMLHttpRequest.
How to set this error: Ensure the asking URL is correct and it respects the same-origin policy. A good mode to find the offending lawmaking is to expect at the URL in the error message and detect it from your code.
InvalidStateError: An endeavor was made to use an object that is not, or is no longer, usable
Related errors: InvalidStateError, DOMException lawmaking 11
Means the code called a part that you should not call at the current country. Occurs normally with XMLHttpRequest
, when attempting to call functions on information technology before it'southward ready.
var xhr = new XMLHttpRequest(); xhr.setRequestHeader('Some-Header', 'val');
In this case, you would get the error considering the setRequestHeader
function can simply be called subsequently calling xhr.open up
.
How to ready this fault: Look at the code on the line pointed by the error and make sure it runs at the correct time, or add whatsoever necessary calls before it (such as xhr.open up
)
Conclusion
JavaScript has some of the most unhelpful errors I've seen, with the exception of the notorious Expected T_PAAMAYIM_NEKUDOTAYIM
in PHP. With more familiarity the errors beginning to make more sense. Modern browsers also help, as they no longer give the completely useless errors they used to.
What's the nearly confusing error you've seen? Share the frustration in the comments!
Want to learn more most these errors and how to prevent them? Notice Bug in JavaScript Automatically with ESLint.
About Jani Hartikainen
Jani Hartikainen has spent over ten years building web applications. His clients include companies similar Nokia and hot super secret startups. When not programming or playing games, Jani writes about JavaScript and high quality code on his site.
codeutopia.netjhartikainenPosts
Source: https://davidwalsh.name/fix-javascript-errors
0 Response to "Uncaught Typeerror: Cannot Read Property 'click' of Null on Keydown"
Post a Comment