While trying to debug I am get the 'length' null error with this line. It is written just like the book instructed, so I don't understand why it is giving me the error? Thanks, =)
if (capital.length < 1) {
( here is the full code as requested.. SORRY)
<script type="text/javascript"> var capital = window.prompt("What is the capital of Missouri?","") if (capital.length < 1) { document.getElementById("firstdiv").innerHTML="Sorry you don't feel like playing.<br /> The Capital of Missouri is Jefferson City."; } else { if (!window.confirm("Is that your final answer?")){ return true; document.getElementById("firstdiv").innerHTML = "The capital of Missouri is: <bold>" + capital + "</bold>, so says you."; } else{ return false; } } </script>
The "Cannot read property 'length' of null" error occurs when accessing the length property on a null value. To solve the error, make sure to only access the length property on data types that implement it - arrays and strings.
The "Cannot read property 'value' of null" error occurs when: trying to access the value property on a null value, e.g. after calling getElementById with an invalid identifier. inserting the JS script tag before the DOM elements have been declared.
TypeError: Cannot read property 'length' of undefined. TypeError is a subset of JavaScript Error that is thrown when code attempts to do something that does not exist on the target object. This message indicates that our code expects to have an object with a length property, but that object was not present.
What is TypeError: Cannot read property ‘length’ of null? How to fix TypeError: Cannot read property ‘length’ of null? The TypeError: Cannot read property ‘length’ of null occurs if you are trying to get the length of an object that is null.
typeerror cannot read property ‘length’ of undefined javascript. Typeerror cannot read property ‘length’ of undefined error in javascript usually occurs when you define a normal variable instead of array variable.
When you are trying to read the property length, the system cant as it is trying to deference a null variable. You need to define capital. Show activity on this post. Show activity on this post. Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share your research! But avoid …
The cannot read property of null error occurs in JavaScript by: Calling a “click ()” function on a “null” value i.e DOM element does not exists. Give the JS script tag before the DOM elements are declared in HTML. Let’s look at the error and then see how we can rectify the issue by understanding the examples below.
The proper test is:
if (capital != null && capital.length < 1) {
This ensures that capital
is always non null, when you perform the length check.
Also, as the comments suggest, capital
is null
because you never initialize it.
This also works - evaluate, if capital is defined. If not, this means, that capital is undefined or null (or other value, that evaluates to false in js)
if (capital && capital.length < 1) {do your stuff}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With