fredley
fredley

Reputation: 33901

TypeError: Cannot read property '0' of undefined

When I call renderFrame, trying to access newLine[i] throws an error of

Uncaught TypeError: Cannot read property '0' of undefined

Why is this? I've included the relevant code.

rows : ["-","-","-","-","-","-","-","-","-","-",],

generateLine : function(){
    var result = new Array(10);
    for(var i = 0; i < result.length; i++){
        result[i] = " ";
    }
},

renderFrame : function(){
    var newLine = this.generateLine();
    for(var i = 0; i < this.rows.length; i++){
        console.log(newLine[i]);
    }
},

Upvotes: 4

Views: 44915

Answers (1)

Halcyon
Halcyon

Reputation: 57719

You forgot

return result;

at the end of generateLine()

So newLine is now undefined. Hence Cannot read property '0' of undefined

Upvotes: 17

Related Questions