开发者

Flex: How can I catch something before I get the error "Cannot access a property or method of a null object reference"

开发者 https://www.devze.com 2023-04-11 14:47 出处:网络
So, I have a dataprovider that, when the module isn\'t be used, is set to an empty arrayCollection.Then, when the module is ready to be used, the dataprovider is changed to an array collection full of

So, I have a dataprovider that, when the module isn't be used, is set to an empty arrayCollection. Then, when the module is ready to be used, the dataprovider is changed to an array collection full of data. For some reason, another one of my functions is having problems with this. I keep getting the following error:

Cannot access a property or method of a null object reference

The following is the line of code causing the error:

for (i = 0; i < pickupPhoto.length; i++)

Is there any way I can make sure that pickupPhoto has a length property before calling this for loop? I tried the following, but I got the same error:

if (pickupPhoto.hasOwnProperty("length"))

Also tried:

if (pickupPhoto.length)

Thanks in ad开发者_JAVA百科vance, Brds


if (pickupPhoto) {
    for (i = 0; i < pickupPhoto.length; i++) {
        /* ... */
    }
}

Also, prefer storing the length in a variable instead of calling the length getter for every iteration:

var len:int = pickupPhoto.length;
for (i = 0; i < len; i++) {
    /* ... */
}


Your problem is not that "length" doesn't exist, but that pickupPhoto is actually null. But, you can check for both:

if (pickupPhoto && "length" in pickupPhoto) {
    // do something with pickupPhoto.length
}
0

精彩评论

暂无评论...
验证码 换一张
取 消