--- title: 'TypeError: can''t access property "x" of "y"' slug: Web/JavaScript/Reference/Errors/Cant_access_property tags: - Fehler - JavaScript - TypeError translation_of: Web/JavaScript/Reference/Errors/Cant_access_property original_slug: Web/JavaScript/Reference/Fehler/Cant_access_property ---
{{jsSidebar("Errors")}}

Fehlermeldung

TypeError: Unable to get property {x} of undefined or null reference (Edge)
TypeError: can't access property {x} of {y} (Firefox)
TypeError: {y} is undefined, can't access property {x} of it (Firefox)
TypeError: {y} is null, can't access property {x} of it (Firefox)

Beispiele:
TypeError: x is undefined, can't access property "prop" of it
TypeError: x is null, can't access property "prop" of it
TypeError: can't access property "prop" of undefined
TypeError: can't access property "prop" of null

Fehlertyp

{{jsxref("TypeError")}}.

Was ist falsch gelaufen?

The Attributzugriff erfolgte über einen {{jsxref("undefined")}} oder {{jsxref("null")}} Wert

Beispiele

Invalide Fälle

// undefined und null sind Fälle auf denen die Methode substring nicht aufgerufen werden kann
var foo = undefined;
foo.substring(1); // TypeError: x is undefined, can't access property "substring" of it

var foo = null;
foo.substring(1); // TypeError: x is null, can't access property "substring" of it

Fehlerbehebung

Um den null pointer auf undefined oder null Werte zu beheben, kann beispielsweise der typeof Operator verwendet werden.

if (typeof foo !== 'undefined') {
  // Hier wissen wir, dass foo definiert ist
}

Siehe auch