# removeChild problem

**URL:** https://forum.kirupa.com/t/removechild-problem/238369
**Category:** flash
**Created:** [September 10, 2007, 4:49pm UTC](https://forum.kirupa.com/t/removechild-problem/238369 "2007-09-10T16:49:32Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![couchObject](https://avatars.discourse-cdn.com/v4/letter/c/a698b9/32.png) [@couchObject](https://forum.kirupa.com/u/couchObject)
#### Post date: [September 10, 2007, 4:49pm UTC](https://forum.kirupa.com/t/removechild-problem/238369/1 "2007-09-10T16:49:32Z")

</div>

Hi,

please read the following example code:

var n:int = 0;  
function setDotz():void  
{  
var dot = new point();  
var intstanceName:String = “p\_mc”+n;  
dot.name = intstanceName;  
this.container\_mc.addChild(dot);

var target:DisplayObject = this.container\_mc.getChildByName(intstanceName);  
trace(target.name);  
this.container\_mc.removeChild(target);  
/\*  
The 3 lines above work here.  
\*/  
n++;  
}  
function removeDotz(i:int)  
{  
var intstanceName:String = “p\_mc”+i;  
var target:DisplayObject = this.container\_mc.getChildByName(intstanceName);  
trace(target.name);  
this.container\_mc.removeChild(target);

/_Here it does not work. Although the trace(target.name) gives the correct result,  
this.container\_mc.removeChild(target) fails with the Error #1009:  
Cannot access a property or method of a null object reference._/

}  
for (var k:int = 0; k \<=20; k++)  
{  
setDotz();  
if (n \>=20)  
{  
for (var i:int = 0; i \<=20; i++)  
{  
removeDotz(i);  
}  
}  
}

I have no idea why in the removeDotz function i can get access to the childs, but i cannot  
remove them, whereas that works in the setDotz function.

Any ideas ?
