We help IT Professionals succeed at work.

'name' is null or not an object

gautam_reddyc
gautam_reddyc used Ask the Experts™
on
Hello I continuously keep getting this error in IE, while everything works in FF. I am trying to rename elements while I swap rows. All my elements have IDs. All elements except for divs, do not have names, so I get all elements by tag name from a row and perform my processing. But here in IE it gives me a problem when I try to replace name, for elements where name is null, I have a condition to prevent making any change, but this error pops up at the condition. Is there any way around for this problem??
var row= document.getElementById(row.id);
var elems = row.getElementsByTagName('*');
var r = explodeArray(row.id, "_");
var vtLineNum = Number(r[1]);
var vtName = r[0];
	for(var i in elems)
	{
		if(elems!=null &&  elems[i]!=null && elems[i].id!=null && elems[i].id!='')
		{		
				document.write(elems[i].name+"<br>");
				elems[i].id = replaceNum(elems[i].id, vtLineNum);
				if(elems[i].name !=null && elems[i].name !='')
					{elems[i].name = replaceNum(elems[i].name, vtLineNum);}

				if(elems[i].id.match("div"+vtName+'_'))
					{					
					var children = document.getElementById(elems[i].id).childNodes;
						for(var i in children)		
						{	
							if(children[i].href != null)			
							{
							children[i].href = replaceNum(children[i].href, vtLineNum);
							}
						}			
					}				
		}
	}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
which line is giving this error?

Author

Commented:
Line 12!! where the if condition checks if the name attribute is not null.

Author

Commented:
it is giving me the error "undefined is null or object"?? It works find in FF. I have attached the changed code. I have used getAttribute function to get data and setAttribute to set data. I am getting the error at the if loop condition where it checks if the attribute is not equal to null.
function swapElemData(row)
{
swapLineData(row);
var row= document.getElementById(row.id);
var elems = row.getElementsByTagName('*');
var r = explodeArray(row.id, "_");
var vtLineNum = Number(r[1]);
var vtName = r[0];
	for(var i in elems)
	{
		if(elems!=null &&  elems[i]!=null && elems[i].id!=null && elems[i].id!='')
		{		
			elems[i].id = replaceNum(elems[i].id, vtLineNum);
			if(elems[i].getAttribute('name') !=null && elems[i].getAttribute('name') !='')
				{elems[i].setAttribute(('name'), replaceNum(elems[i].getAttribute('name'), vtLineNum));}

			if(elems[i].id.match("div"+vtName+'_'))
				{	
					if(document.getElementById(elems[i].id) !=null)
					{
					var children = document.getElementById(elems[i].id).childNodes;
						
						for(var i in children)		
						{	
							if(children[i].href != null)			
							{
							children[i].href = replaceNum(children[i].href, vtLineNum);
							}
						}			
					}
				}
			
		}
	}
}

Open in new window

again, which line number?
all the elements will not have name attribute assigned to them

Author

Commented:
line 14!! yes you are right, how would I know if the element has the attribute??
can you alert these 2 statements
alert(elems[i])
alert(elems[i].id)
alert(elems[i].getAttribute('name'))

Author

Commented:
I know this is embarrasing, but IE is giving me the error "permission denied" everything was working till now, atleast giving me errors in logic. Now it pops an error at line 9 and tells me permission denied!! Who the hell are Microsoft helping with their senseless rules!!
<<Who the hell are Microsoft helping with their senseless rules!!>>
priceless :)...try closing your browser and start again.

Author

Commented:
Umm no luck, I have restarted my laptop, it gives me the same error. I actually am developing on a linux system and testing on a windows system which is in the same network. Is that ok, or could that be an issue??
yes, it should be ok
When it says 'permission denied', does it give any hint on which line?

Author

Commented:
Yes!! it is giving me an error at line 9 and an arrow points towards the variable elems!! I have also checked if elems was null, it is not null, but I still get the same permission denied error!!

Author

Commented:
Ok one thing that I know now is "elems" is an array object and I am able to get its length. I have tried using for in loop, for loop and while to print out its elements but all I get back is permission denied. When I do not traverse through the array object "elems", everything works fine, but when I try to traverse through that array, I am getting the error.
hey I have solved the issue!! It is just that IE was being bitch and had to be pampered with extra variables. It wasn't allowing me use the array variables and keys directly. Also for the problem with elem[i].name, I had to come up with an extra if loop and a specific condition for that.

Thanks a lot gurvinder for being patient and helping me with possible solutions.
function swapElemData(row)
{
swapLineData(row);
var row= document.getElementById(row.id);
var elems = row.getElementsByTagName('*');
var r = explodeArray(row.id, "_");
var vtLineNum = Number(r[1]);
var vtName = r[0];

	for(var i in elems)
	{
		if(elems[i]!=null && elems[i].id!=null && elems[i].id!='')
		{		
			var tempElementId = replaceNum(elems[i].id, vtLineNum); 
			elems[i].id = tempElementId;
			if(tempElementId!= null && tempElementId.match("div"+vtName+'_'))
				{
					var children = document.getElementById(tempElementId).childNodes;
					
					for(var i in children)		
					{	
						if(children[i].href != null)			
						{
						var child = replaceNum(children[i].href, vtLineNum);
						children[i].href = child;
						}
					}			
					
				}

		}
		if(elems[i]!=null && elems[i].name!=null && elems[i].name!='')
		{
			elems[i].name = replaceNum(elems[i].name, vtLineNum);
		}
	}
}

Open in new window

Michel PlungjanIT Expert
Top Expert 2009

Commented:
Can you try this for fun

if(elems[i] && elems[i].id)