"Cannot read property 'xxx' of null"

Matrym picture Matrym · Apr 26, 2010 · Viewed 52.2k times · Source

I've got a document fragment with children that I want to loop over (when possible). This is causing an error "Cannot read property 'xxx' of null".

How do I test if this will be the case?

Answer

Daniel Vassallo picture Daniel Vassallo · Apr 26, 2010

You may want to do the following:

if ((documentFragment !== null) && documentFragment.hasOwnProperty('xxx')) {
    // handle property xxx of documentFragment as required
}