Thursday, November 26, 2009

OpenLayers - fixing "element is null" error

I've had a hard time trying to debug a problem on a Vector layer that had tooltips as well as regular popups. It worked fine but from time to time Firebug would log an error message like the following:


I'm running OpenLayers 2.8. This error messed up javascript - zooming and panning were broken, the current popup would remain un-close-able, but other markers seemed to work just fine.

I managed to track this problem to a null element in Element.js. I'm not sure why the element is null at that point, but it may be a safe thing to do to check if the element exists before trying to change its style...

I attached a patch that should solve this (adds an if before setting the style):


--- lib/OpenLayers/BaseTypes/Element.js.original 2009-08-04 15:42:45.000000000 +0300
+++ lib/OpenLayers/BaseTypes/Element.js 2009-08-04 15:40:43.000000000 +0300
@@ -47,7 +47,9 @@
hide: function() {
for (var i=0, len=arguments.length; i<len; i++) {
var element = OpenLayers.Util.getElement(arguments[i]);
- element.style.display = 'none';
+ if(element){
+ element.style.display = 'none';
+ }
}
},

@@ -61,7 +63,9 @@
show: function() {
for (var i=0, len=arguments.length; i<len; i++) {
var element = OpenLayers.Util.getElement(arguments[i]);
- element.style.display = '';
+ if(element){
+ element.style.display = '';
+ }
}
},


Of course, after applying the patch you need to re-build OpenLayers.js for this to work

No comments: