'null' is null or not an object

Wer kennt sie nicht, diese durchaus philosophische aber dennoch selten hilfreiche Fehlermeldung des Internetexplorers von Microsoft (wer die Meldung nicht sofort versteht, sollte sich einen Moment Zeit nehmen, um die Tiefe dieser Nachricht auf sich wirken zu lassen)?

Logik: eine Oder-Aussage ist wahr, wenn eine der beiden Aussagen wahr ist.
Nun ist "null" sicherlich null. Also ist die Gesamtaussage "null is null or noch an object" logisch gesehen richtig. Genauso ist - logisch gesehen - aber auch "null ist null oder im Sommer ist es kalt" richtig. Denn logisch gesehen reicht ja schon die Wahrheit der ersten Teil der Aussage.
Der Teil "or is not an object" ist also wenig hilfreich. Da könnte alles stehen, denn null ist sicherlich null. Wer weiß das nicht.

Alternativ gibt es übrigens auch:
'undefined' is null or not an object
Hier ist eigentlich die entscheidende Frage: "Ist undefined null"? Ich würde sagen: Nein. Daher muß logisch undefined kein Objekt sein. Das würde ich schon eher glauben.

Die entscheidende Frage ist aber: Was soll das? Warum quatscht mich der IE mit solchen eher philosophischen Aussagen voll? Ist das für irgendetwas hilfreich?

Diese Meldungen zeigte auch schon der nunmehr 10 Jahre alte Internet Explorer 6. Vielleicht sogar die noch ältere Version 5. Aber Microsoft ändert nichts ohne Not ... und so genießen wir diese Meldung auch noch im aktuellen Internet Explorer 8.

Ich habe ja ein gewisses Verständnis dafür, daß der IE8 "abwärzkompatibel" mit dem IE6 sein muß (also die gleichen Fehler enthalten muß). Schließlich sollen für IE6 "optimierte" Websites auch noch mit IE8 angezeigt werden können.

Aber ...
Kann das Megaunternehmen Microsoft mit tausenden von Entwicklern nicht vielleicht mal einen Azubi einstellen, der den IE-Code durchgeht und für hilfreichere Fehlermeldungen sorgt? Ich schätze, 3 Monate sollten dafür reichen. So schwer kann das doch nicht sein. Ja, das kostet dann 0.00000000000001% des Windows 7 Umsatzes. Aber wenn es niemand sonst tut: ich finanziere das gerne auch höchst persönlich.
Sinnvolle Fehlermeldungen brechen wirklich keinen Website-Code. Sie sind einfach nur hilfreich. Absolut nicht schädlich. Sie stören niemanden.
Comments:

trackback from BlogHouse Montag, November 09, 2009 08:00 AM

anderen geht es auch so

Nicht nur ich rege mich in regelmäßigen Abständen über unser aller Werkzeug des Teufels, den Internet Explorer, auf. Nein es tun andere ebenso.

Wolfgang Schmidetzki findet passende Worte zu den Javascript Fehlermeldungen im IE: Internet Explorer sucks...

nitro2k01 Samstag, November 14, 2009 10:51 PM

(The formatting in the above post is messed, so I'm reposting with fixed formatting. You can delete the first comment.)

Let me shed some light on this message. First it's important to understand what objects are in Javascript, they are hash lists, or in other words associative arrays. If you do this for example...

var myObject = {};
myObject.property = 42;

...you are first creating a new object which you reference as myObject. Then you're giving that object a property named property. But this is no different from this code...

 var myObject = {};
 myObject['property'] = 42;

...where you're storing the property in the form of an associative array element.

So what about the error then? That error happens when the script tries to reference an associative array element from something that isn't an associative array. And there actually only two thing in JS that are not associative arrays, and that is the special values null and undefined. You might think that strings and numbers are also not associative arrays, but this is not the case. Even if you can't modify the properties of a number, there are still properties that can be used, most notably the function toString. For example...

 alert((42).toString())

...or even, using the other syntax:

 alert((42)['toString']())

So we're left with two things that are not associative arrays, again null and undefined, which thus will create this error. This code will trigger this error:

 alert(undefined.test);
 alert(null.test);

However, this code will not fail with same message:

 alert(nonExistentVariable.test);

It will fail already at the evalution of nonExistentVariable and complain that "nonExistentVariable is undefined". However, this code will still generate the error, because the function argument x is undefined and then you try to access an array element in it. x is undefined, so it is indeed either null or not an object.

 function test(x){
     alert(x.y);
 }
 test();

I hope this cleared up what this message means. Of course, this problem is secondary. The real problem is how a variable ended up being undefined or null, which probably happens because the browser doesn't have support for some particular function. The message itself exists on all browsers, but with different wording.

Opera has the almost as bad message: "Cannot convert undefined or null to Object" while Firefox has the much better "undefined has no properties".

Wolfgang Schmidetzki Samstag, November 14, 2009 11:42 PM

nitro2k01: wow, what and excelent description of the origin of that kind of error message in IE-javascript. Thanks for that.

However ... the message itself is not very helpfull. What would help f. e. ist the correct name of the var that causes the error.

Calling your sample test() in IE gives the message: y is null or not an object". That is irritating bc. the problem is "x" and not "y".

If you call test() in FireFox, you will get the message: "x is undefined" what is correct.

More badly:
Change the body of your function test() to alert(x.y()) and you will get the meaningless message "undefined is null or not an object" from IE, while FF will say "x is undefined" witch is mutch more correct and helpfull.

Enter Your Comment:

use markdown syntax