You probably have put your code in the <head></head>
section of your file and your JavaScript code can't access the DOM elements since the DOM hasn't been created yet.
If your code is in the head, wrap it with:
window.onload = function() {
// your code
}
Otherwise put it right before the closing body tag. This is the recommended way of including JavaScript since the essential content gets loaded first.