This error means JavaScript tried to read a property off a value that is null. Null is not an accident of timing in the way undefined usually is, something set this value to nothing on purpose, or a lookup that returns null by design found no match. The overwhelmingly common case on web pages: document.getElementById() or querySelector() found no element and returned null, and the very next property access threw.
(reading 'addEventListener') or (reading 'value') after a DOM lookup is the classic fingerprint of a missing element.document.getElementById('the-id'). If it returns null there too, the element genuinely is not in the page at that moment.useState(null) fetched data, a ref that has not attached yet (ref.current is null before mount and after unmount).defer attribute, or wrap setup code in a DOMContentLoaded listener. One of these fixes most occurrences outright.getElementById takes no # while querySelector requires it.const el = document.querySelector(sel); if (el) { ... }. Treat a null lookup as a normal outcome, not an impossibility.Why does getElementById return null when the element is right there in the HTML? Timing, almost always. The script executed before the parser reached that element, typically a script tag in the head without defer. View-source shows the final document, not the document as it existed when the script ran.
What is the difference between this and “cannot read properties of undefined”? The shape of the bug is identical, only the empty value differs. Null points at deliberate emptiness or failed lookups, undefined points at values that were never assigned. The distinction often identifies the culprit in one step.
Does optional chaining fix it? user?.name stops the throw, but if the element or object was supposed to exist, the feature then silently does nothing. Guard where absence is legitimate, fix the lookup or the timing where it is not.