0

After compiling and running my program, i run into runtime error 1010:

TypeError: Error #1010: A term is undefined and has no properties.
at DC/updateScore()[DC::frame74:36]

This is the piece of code:

function updateScore(e:MouseEvent) {
var i:uint=0;
for(;i<balls.length;i++)
    if(balls[i]==e.target)
        break;
if(balls[i].isCorrect) {
    score++;
    timeField.text = new String(score);
}
else {
    score--;
    timeField.text = new String(score);
}
}

What's the problem? I'm using updateScore function for MouseEvent listener, as you can see.

nicks
  • 2,161
  • 8
  • 49
  • 101

6 Answers6

3

Please put { and } to your for loop!

function updateScore(e:MouseEvent) {
    var i:uint=0;
    for(;i<balls.length;i++) {
            if(balls[i]==e.target)
                break;
        if(balls[i].isCorrect) {
            score++;
            timeField.text = new String(score);
        }
        else {
            score--;
            timeField.text = new String(score);
        }
    }
}

Its allowed not to use { and } with a loop but then only the first statement will be excuted.

Kaken Bok
  • 3,395
  • 1
  • 19
  • 21
1

The error means that you are trying to manipulate properties of a undefined variable. You are probably trying to access balls[i] when there is no element on the i-th position. Or possibly there is another object without the property you want to access.

At what line do you get the error. Are you sure balls[i].isCorrect is a valid property.

Rob Fox
  • 5,355
  • 7
  • 37
  • 63
1

@Jens answer has it, but his explanation is confusing.

When your loop exits,

i = balls.length

On the next line, you're referencing

balls[i].isCorrect.

On that particular line, i is 1 greater than the greatest index in balls. Hence, exception.

While you may not want each of those lines to execute for each repetition of the loop as Jens thought, you've got to address this.

Sam DeHaan
  • 10,246
  • 2
  • 40
  • 48
0

You've missed the first rule in your for loop:

for(;i<balls.length;i++)

I think you meant

for(i;i<balls.length;i++)
shanethehat
  • 15,460
  • 11
  • 57
  • 87
  • it's not the point. it's legal. anyway, problem existed before i deleted the first rule in the loop. – nicks Aug 02 '11 at 09:58
  • 1
    Have you checked that after you break the loop balls[i] actually contains something? – shanethehat Aug 02 '11 at 10:00
  • why? is that possible to balls[i] not to contain anything? i'm perfectly sure it does. – nicks Aug 02 '11 at 10:02
  • 1
    if `balls[i]==e.target` never equals true, then `i` will be incremented beyond the available indexes of your array – shanethehat Aug 02 '11 at 10:03
  • that's also impossible. i'm perfectly sure about that. – nicks Aug 02 '11 at 10:04
  • 1
    Original answer was useless, tempting me to -1, but comments are useful. Nika, you may be *absolutely sure*, but it's the *only* source of potential error in your code. – Sam DeHaan Aug 02 '11 at 12:33
  • @sam - I won't edit because the comments would cease to make sense. Best it drops to the bottom and you and Jens get voted up. Feel free to -1. – shanethehat Aug 02 '11 at 12:38
0

try adding

if(!balls){
   trace('balls missing');
   return;
}

before loop declaration

www0z0k
  • 4,444
  • 3
  • 27
  • 32
0

based on the little code you've provided, you've either not instantiated the balls vector or the timeFiled textField.

var balls:Vector<Ball>;
balls.push(new Ball(whatever), new Ball(whatever), new Ball(whatever));
trace(balls);

//ERROR
//you must initialize the vector:  var vector:Vector<Ball> = new <Ball>;

which part of the code is on line 36?

the problem may also be with the Ball object's isCorrect property.

Chunky Chunk
  • 16,553
  • 15
  • 84
  • 162