John Cooper
John Cooper

Reputation: 7651

Checking for Null in an Object

for ( var i=0; i<MyArrayObj.length; i++ ) {

}

I am getting an error called, Cannot call MyArrayObj, since its is NULL

Upvotes: 0

Views: 145

Answers (4)

ben
ben

Reputation: 472

So that means you must not have initialized myArrayObj yet. You have to put something in the array before you can iterate through it.

Upvotes: 0

Shadow Wizzard
Shadow Wizzard

Reputation: 66396

You can also add this line above your loop just to be sure and avoid having to check at all:

MyArrayObj = MyArrayObj || [];

This will assign empty array into the variable in case it's null or undefined.

Upvotes: 3

Tjekkles
Tjekkles

Reputation: 5622

Did you declare the MyArrayObj and assigned a correct value to it?

Could you provide the code where you assign the array to a value?

Otherwise check if the array is not null first.

Upvotes: 1

tskuzzy
tskuzzy

Reputation: 36476

Assuming there isn't a logical error with MyArrayObj being null:

if(MyArrayObj) {
    for ( var i=0; i<MyArrayObj.length; i++ ) {

    }
}

Upvotes: 11

Related Questions