Skip to main content
Inspiring
March 27, 2009
Question

How to Remove Null Object Reference Error

  • March 27, 2009
  • 3 replies
  • 1195 views
I am trying to figure out the logic to remove a null reference error that is popping up when an item is not dragged. In the code, I have a stopDrag setup for the stage. It works as expected unless the user clicks the item but doesn't drag it. It causes the variable I have set to bring the name of the dropTarget to be a null which crashes the system. I have tried to declare the variable with a default value but it still doesn't work.

It is the targetStop code that it is crashing on. Is there anyway to default a value so I can do an if / then statement to determine if there is a value? I've tried: if (targetStop == null) but that doesn't seem to work.

Here is the error I am getting: TypeError: Error #1009: Cannot access a property or method of a null object reference.

Any help would be greatly appreciated.

Thanks.

Dave
This topic has been closed for replies.

3 replies

Inspiring
March 27, 2009
Still not working...the problem is that there is no "dropTarget.name" because I haven't dragged anything and it won't take setting a variable.

here's a question...is there a variable or event that is fired when something is dragged? I could listen for that event and base my code on that. They have startDrag() and stopDrag()...is there anything for the middle of the process like isBeingDragged() or hasBeenDragged().

Thanks.

Dave
Inspiring
March 27, 2009
No, there no such things as isBeenDragged. I think there is a design issue here. What are you trying to accomplish. How you variable targetStop is used? Why do you need it?
Inspiring
March 27, 2009
I need to determine where the movieclip was dropped so that I can go to the frame associated with the selection. I am using the drag like a menu selection so that when you drop on that menu item, you go to that frame. To do that, I need to know what it was dropped on. The targetStop gets the dropTarget.name for me.

There's no doubt in my mind that it is a design issue :-)

I have removed the variable to get the droptarget.name and I am getting the value from the hitTestObject and it appears to be working now...I will test further and see what happens.

Thank you for all the help.

Dave

Inspiring
March 27, 2009
Kurrykid wrote:

> stage.addEventListener(MouseEvent.MOUSE_UP, testStopDrag);
> function testStopDrag(event:MouseEvent):void{
> test_mc.stopDrag();
> var targetStop:String = test_mc.dropTarget.name;
>

Try to set an "if" around it.

if (test_mc != null){
test_mc.stopDrag();
var targetStop:String = test_mc.dropTarget.name;
}

You might set test_mc to null before draging, so it has an initial status.
Inspiring
March 27, 2009
Try to remove the object from stopDrag - it really doesn't matter what stops dragging because only one thing at a time can be dragged anyway and you listener is attached to the stage - no references to non-existing objects:

function testStopDrag(event:MouseEvent):void{
stopDrag();
.....
}
Inspiring
March 27, 2009
Good idea...I have changed it to stopDrag(); However I am still getting the error becuase the variable below it (targetStop) gets the value of the item that was dragged. I need to capture this data if something is dragged.

Any other thoughts?

Thanks.

Dave
Inspiring
March 27, 2009
Because I have no idea how you use this variable, it is hard to say what to do with it. I am not sure why would you make the assignment like this at all. The reason for my doubt is that, again, dragging has a singular target. So, the same object that is dragged is the one that is dropped, correct? So, why not to make this assignment inside the listener that starts dragging - not the one that stops it?