Properties of variables with null or undefined values should not be accessed

  • NULL_POINTER
  • Error
  • High
  • cwe

This rule applies when properties of variables with null or undefined values are accessed.

Trying to access properties of null or undefined variables causes a TypeError exception.

Noncompliant Code Example

View with compliant examples side by side
// Example 1
function foo() {
    var obj;
    return obj.x; // NULL_POINTER alarm
}

// Example 2
if (x == null) { // (x == null) should be modified to (x != null).
    y = x.a; // NULL_POINTER alarm: x is undefined or null but is property-accessed.
}

// Example 3
y = x || x.a; // NULL_POINTER alarm: x has a falsy value but is property-accessed.

Compliant Code Example

View with noncompliant examples side by side
// Example 1
function foo() {
    var obj = { x: 1 }; // 'obj' should be initialized.
    return obj.x;
}

// Example 2
if (x != null) {
    y = x.a;
}

// Example 3
y = x && x.a;

Version

This rule was introduced in DeepScan 1.0.0-alpha.

See

Was this documentation helpful?