Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'length' of null (javascript)

Tags:

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>  
like image 315
JBSAMOM Avatar asked Mar 31 '13 16:03

JBSAMOM


People also ask

Can't access property length is null?

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.

Can you read property of null?

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.

What does Cannot read property length of undefined mean?

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?

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.

Can not read property ‘length’ of undefined in JavaScript?

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.

Why can't the system read the length of a null 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 …

How to fix cannot read property of null error in JavaScript?

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.


2 Answers

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.

like image 189
Hunter McMillen Avatar answered Sep 29 '22 22:09

Hunter McMillen


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} 
like image 23
Kamil Naja Avatar answered Sep 30 '22 00:09

Kamil Naja