# How to avoid a #1009 error in a drag and drop

**URL:** <https://forum.kirupa.com/t/how-to-avoid-a-1009-error-in-a-drag-and-drop/329161>\
**Category:** flash\
**Created:** [July 26, 2012, 8:42pm UTC](https://forum.kirupa.com/t/how-to-avoid-a-1009-error-in-a-drag-and-drop/329161 "2012-07-26T20:42:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![m2244](https://avatars.discourse-cdn.com/v4/letter/m/ac91a4/32.png) [@m2244](https://forum.kirupa.com/u/m2244)\
**Post date:** [July 26, 2012, 8:42pm UTC](https://forum.kirupa.com/t/how-to-avoid-a-1009-error-in-a-drag-and-drop/329161/1 "2012-07-26T20:42:00Z")

</div>

OK. I’ve got this drag and drop working but when I drop it anywhere other thatn the target, such as the stage, I get this error.

```auto

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

```

The problem is that it depends on how I drop the answer. If the mouse is directly over the target, everything is fine. But if the mouse is off the target, the drop is considered a miss. Is there a way to avoid this?

```auto

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.utils.Dictionary;
var targetArray:Array = [];
stop();

 / *************Set up the questions with text from the XML file.********************* /
 for(var i:int = 0; i < xmlFile.questions.question.length(); i++)
 {   
  var quesBack:mcQuesBack = new mcQuesBack();
  quesBack.x = 30;
  quesBack.y = 30 + (i * 80);
  quesBack.name = String(i);
  quesBack.mouseChildren = false;
  quesBack.label = xmlFile.questions.question*.answer; 
 
  quesBack.quesText.text = xmlFile.questions.question*.content;
  
  stage.addChild(quesBack);
  targetArray* = quesBack;
 }
 
 / *************Set up the draggers with text from the XML file.********************* /
 for(var n:int = 0; n < xmlFile.draggers.answer.length(); n++)
 {
  var ansBacking:mcDragger = new mcDragger();
  ansBacking.x = 650;
  ansBacking.y = 30 + (n * 70);
  ansBacking.buttonMode = true;
  ansBacking.txtAnswer.text = xmlFile.draggers.answer[n].content;
  ansBacking.mouseChildren = false;
  ansBacking.name = xmlFile.draggers.answer[n].content;
  ansBacking.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
  //ansBacking.addEventListener(MouseEvent.ROLL_OVER, handleAlphaOver);
  stage.addChild(ansBacking);
 }
 
function startDragging(evt:MouseEvent):void
{
 evt.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, startDragging);
 //evt.target.addEventListener(Event.ENTER_FRAME, checkHitTest);
 origLocatX = evt.currentTarget.x;
 origLocatY = evt.currentTarget.y;
 evt.currentTarget.startDrag(false);
 evt.currentTarget.parent.addChild(evt.currentTarget);
 evt.currentTarget.addEventListener(MouseEvent.MOUSE_UP, onDrop);
}
function onDrop(evt:MouseEvent):void
{
 
 evt.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, onDrop);
 evt.currentTarget.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
 
 evt.currentTarget.stopDrag();
 
 if(xmlFile.draggers.@multiUse == true)
 {
  evt.currentTarget.x = origLocatX;
  evt.currentTarget.y = origLocatY;
  
 }
 else
 {
  stage.removeChild(DisplayObject(evt.target));
 }
 
 var indexOfCurrentTarget:int = targetArray.indexOf(evt.currentTarget.dropTarget.parent);
 if(evt.currentTarget.dropTarget.hitTestObject(targetArray[indexOfCurrentTarget]))
 {
  targetArray[indexOfCurrentTarget].quesText.text += " " + evt.currentTarget.txtAnswer.text;
 }
}

```
