Explain briefly touched on the probable cause of the problem in his answer, and this means that it this.units[i]can be zero. If you try to access the property by a null value, you will get a "null or not object" error. In your example, this is done by trying to access the this.units[i].xif statement. The safest thing to do is check and see if this is null first:
for(var i in this.units)
{
if (this.units[i] === null)
continue;
if(this.units[i].x==att.x && this.units[i].y==att.y){}
}
, , for...in .