Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects Accessing Object Properties

Uncaught TypeError: Cannot set property 'innerHTML' of null at print (script.js:29) at script.js:32?

var student={ name:'osnan', age :23, batch:'1511a', skills:['html','css','ms office'], country:'us'


function print( message){
var div=document.getElementById('a');


print(student.age); //html <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> <script src="script.js"></script> </head>

<body> <div id="a"></div> </body> </html>

2 Answers

Konrad Traczyk
Konrad Traczyk
22,287 Points

Hello, your script runs before all DOM has been loaded, so basiclly you trying to get element which isn't parsed yet that's why you get error.

Place your script tags after body closing tag thats ensures you that whole document has been already parsed when the script starts executing.

<!doctype html>
        <meta charset="utf-8">
        <title>Untitled Document</title>
        <div id="a"></div>
    <script src="script.js"></script>

Hope it helps.

thanks :)

5,912 Points

Just wanted to say thanks too. As this solution helped me with another project :)

thanks. helped me from pulling my hair out in frustration