I ran into this problem on the JavaScript for this website where on some pages it wasn't
Fixing it is really easy. You need to perform a null check, so by wrapping the line within an if statement verifying it isn't null, you'll prevent this error from occurring.