Error of the Day · Developers

Null references: the billion-dollar mistake you still ship

In 2009 the inventor of the null reference called it his billion-dollar mistake. Here's why null keeps crashing production code, and the type-system fixes that turn it into a compile-time error.

Wronguser = findUser(id); print(user.name) // assumes a user always comes back

Rightuser = findUser(id); if (user != null) print(user.name) else handleMissing() // or let the type system force the check

Memory trick If a value can be missing, say so in the type; if the type can't say it, check before you dot.

What people get wrong#

The error is treating a reference as if it always points at something. You call findUser(id), you get back what looks like a user, and you read .name. Most days that works. The day the user does not exist, the function returns null and the program falls over:

Exception in thread "main" java.lang.NullPointerException
TypeError: Cannot read properties of undefined (reading 'name')
TypeError: null has no properties

Java documents NullPointerException as being thrown "when an application attempts to use null in a case where an object is required," such as calling a method or reading a field on a null object. MDN lists the browser-specific wordings of the JavaScript version under "null/undefined has no properties." Same bug, different accent.

Why it happens: the billion-dollar mistake#

The null reference has a known inventor. In a talk at QCon London in 2009, the computer scientist Tony Hoare, best known for Quicksort, explained that he introduced null references in the language ALGOL W in the mid-1960s "simply because it was so easy to implement," and called the decision "my billion-dollar mistake" (InfoQ recording and summary).

The figure is a deliberately rough guess, not an audit. According to InfoQ's show notes, Hoare said nobody knows the real cost, but put it at around a billion: more than a tenth of a billion, less than ten billion. The structural problem is what matters. As the notes summarize it, null became "a member of every type," so every reference variable needs a null check on every use, and nothing in most older languages makes you do that check.

That is the trap. A variable typed as User promises a user, but it might actually hold "no user," and the compiler cannot tell the two apart. The bug only shows up at runtime, often far from where the null was produced.

The fix#

val user: User = findUser(id) // compiles, then crashes when no user exists

val user: User? = findUser(id) // the ? makes "maybe missing" part of the type; the compiler forces you to handle it

Make absence visible in the type, and the compiler checks it for you.

Modern languages fix this by splitting "a user" from "maybe a user":

  • Kotlin distinguishes nullable types (String?) from non-null ones, and per the Kotlin docs catches potential null issues "at compile time rather than runtime." The documented ways to still get an NPE include the not-null assertion operator !! and Java interop, which is a hint about where to look first.
  • Rust represents a value that might be missing with Option, which is "either Some and contains a value, or None," and is commonly paired with pattern matching that always accounts for the None case.
  • TypeScript with strictNullChecks on gives null and undefined their own types, so you get a type error when you use them where a concrete value is expected. With it off, the docs warn, they are "effectively ignored by the language."
  • C# has nullable reference types: you declare which variables may hold null, and the compiler warns when your code does not match.

How to check your own code#

Turn the strict setting on (strictNullChecks, C#'s nullable context) and read the warnings rather than suppressing them. Grep for escape hatches like Kotlin's !! or TypeScript's non-null !, since each one is a promise the compiler cannot verify. Decide what "not found" means at the API boundary: return an optional type, throw a specific error, or return an empty collection instead of null for lists. And in languages without these tools, check before you dot.

Sources

Every factual claim on this page links to one of these. Checked November 15, 2026. See how we source.

  1. InfoQ: Null References: The Billion Dollar Mistake (Tony Hoare, QCon London 2009) infoq.com
  2. Oracle Java SE 21 API: NullPointerException docs.oracle.com
  3. MDN: TypeError: null/undefined has no properties developer.mozilla.org
  4. Kotlin docs: Null safety kotlinlang.org
  5. Rust std docs: Module std::option doc.rust-lang.org
  6. TypeScript TSConfig reference: strictNullChecks typescriptlang.org
  7. Microsoft Learn: Nullable reference types (C#) learn.microsoft.com