Error #1009: Cannot access a property or method of a null object reference

With this code I get the error “Error #1009: Cannot access a property or method of a null object reference.” because of the trace. But the same trace inside the if statement doesn’t cause a problem. Any reasons. I can’t refer to the object outside the if statement.

Note: This code is inside an EnterFrame event.


//bolt fire---------------
if (bolt_fire) {
	var bolt_no:Sprite = new heroweapon();
	bolt_no.name="xyz";//k
	addChild(bolt_no);

	bolt_no.x=hero_weapon.x;
	bolt_no.y=hero_weapon.y;
	bolt_no.rotation=angle2;
	angle3=angle2;
	trace(bolt_no.x,bolt_no.y,bolt_no.name);

	bolt_fire=false;
}
trace(bolt_no.x,bolt_no.y,bolt_no.name);