Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Tony Hoare called the null reference a billion-dollar mistake, and SQL NULL has its own long history of confusion. How would you set long-term policy for nulls across data models, APIs and Java code in a large company?

I would start by making clear that SQL NULL, Java null and JSON null are three different things: unknown with three-valued logic, an absent reference, and an explicit null that is itself different from a missing field. The policy is NOT NULL by default in schemas, explicit optionality in API contracts, Optional for return values and JSpecify null-safety annotations checked at build time in Java, with consistent mapping at every boundary. I would measure progress through incidents and data quality checks rather than purity, and accept that legacy schemas stay nullable, protecting them with documentation and safe query patterns.

Background to this question

Every large company pays for nulls, usually in small amounts that nobody adds up: a NullPointerException in production, a report that excludes rows because of three-valued logic, an API client that treats a missing field as a deletion. At the senior level, the interesting question is not how to avoid one NPE but how to set a policy that hundreds of engineers will follow across databases, APIs and code, without starting a crusade that stalls delivery. In this answer, we will separate the three kinds of null, set defaults for each layer, look at the boundaries where meaning gets lost, decide how to measure success, and see how to treat legacy systems pragmatically.

1Three Different Kinds of Null

The first step is shared vocabulary, because people use one word for three ideas. In SQL, NULL is a marker for a missing or unknown value, and it brings three-valued logic: NULL = NULL is UNKNOWN, WHERE drops UNKNOWN rows, aggregates skip NULLs. In Java, null is a reference that points to no object; using it causes a NullPointerException, the problem Tony Hoare described when he introduced the null reference in ALGOL W. In JSON, there is a further distinction: a field can be present with the value null, or be absent altogether, and in a PATCH request those often mean different things, set to empty versus leave unchanged.

These differences matter because the same customer attribute passes through all three. If an engineer believes missing in JSON, null in Java and NULL in SQL are the same thing, the meaning changes silently at every hop.

One missing middle name, three representations
LayerRepresentationBehaviour to remember
PostgreSQLmiddle_name IS NULLcomparisons are UNKNOWN, WHERE drops them
JavaString middleName = nullcalling a method on it throws an NPE
JSON PATCH body"middleName": nullusually means clear the value
JSON PATCH bodyfield absentusually means leave it unchanged

2Setting Defaults for Each Layer

Good policy is mostly a set of defaults, so the safe choice is the one engineers make without thinking. In schemas, columns are NOT NULL unless the author can say what NULL means, and the migration linter flags new nullable columns without a comment. In API contracts, OpenAPI specs list required fields explicitly, and nullable is used only when null is a real value distinct from absent. In Java, methods that may have no result return Optional, while fields and parameters are not Optional; instead, packages are annotated with JSpecify's @NullMarked, so everything is non-null by default and the exceptions carry @Nullable.

Java
@NullMarked // package-info.java: non-null unless marked
package com.example.customers;
public record Customer(String id, String name, @Nullable String middleName) {}
public interface CustomerRepository {
Optional<Customer> findById(String id); // absence is part of the contract
}
String n = customer.middleName().trim(); // NullAway or a JSpecify-aware checker flags this at build time

JSpecify is a set of standard null-safety annotations agreed by several tool and library vendors, and Spring Framework 7 and Spring Boot 4 use it for their own APIs, so our code and the framework speak the same language. A checker such as NullAway, run in the build, turns the annotations into compile errors, which is where this policy gets its teeth.

3Mapping Consistently at the Boundaries

Most null bugs happen where data crosses a boundary, so the policy must say how each boundary maps. The JSON layer distinguishes absent from null for PATCH, using a small wrapper type or JsonNullable, and rejects null for required fields at validation time. The persistence layer maps a nullable column to a @Nullable field and a NOT NULL column to a non-null field, never the other way round. Events and Avro or Protobuf schemas state optionality explicitly, since Protobuf 3 scalar fields have default values instead of null unless they are declared optional.

The reason to write this down is that each team otherwise invents its own mapping, and a missing value ends up meaning unknown in one service, zero in the next and delete in the third.

Step throughA missing value crossing the layers
  1. API receives JSONabsent vs null decided by the contract
  2. Validationrequired fields rejected if null
  3. Domain object@Nullable only where the model allows it
  4. Persistence mappingnullable field <-> nullable column
  5. Event publishedschema states optional fields

5 steps in order. Play them, or step through them one at a time.

4Measuring Impact Instead of Pursuing Purity

A null policy can turn into an endless refactoring project. I would avoid that by measuring the cost first: how many incidents in the last year came from NullPointerExceptions or from NULL logic in queries, how many data quality checks fail because a required attribute is missing, and how much time on-call engineers spent on them. That number decides how much to invest.

Then I would track a few indicators: the share of modules that are @NullMarked and checked in the build, the number of nullable columns added without documentation, and the trend in null-related incidents. If incidents drop, the policy works, even if half the code base is not annotated yet.

5Living With Legacy Schemas

Old schemas will stay nullable for a long time. A table with 300 million rows and 15 consumers is not worth migrating just to satisfy a guideline. For those, the policy is protection rather than change: document what NULL means in each important column, provide reviewed query patterns such as NOT EXISTS instead of NOT IN and IS DISTINCT FROM for change detection, and wrap access in a repository layer that converts nullable columns into Optional or explicit domain values. New tables and new services follow the strict defaults. Over the years, the legacy part shrinks as systems are replaced, and nobody had to stop delivery for it.

6Follow-up questions

Why not use Optional for fields and parameters as well?

Optional was designed as a return type that forces callers to think about absence. As a field it adds an extra object per value, is not serializable, and maps poorly to JPA and JSON. As a parameter it makes calls clumsy and still allows passing null itself. Annotations with a build-time checker give the same safety for fields and parameters at no runtime cost.

How would you get teams to adopt the annotations?

I would start with one or two willing teams, add @NullMarked package by package, and run the checker in warning mode first so the build does not break on day one. Once a package is clean, the checker becomes an error for that package. Shared libraries go first, because annotating them gives every consumer better warnings immediately.

What about Kotlin services?

Kotlin builds null safety into its type system and understands JSpecify annotations on Java APIs, so the policy fits naturally. The boundary rules still apply, because Kotlin's types cannot fix a JSON contract or a nullable column whose meaning is unclear.

7Conclusion

In this answer, we separated SQL NULL, Java null and JSON null, set safe defaults for each layer with NOT NULL schemas, explicit API optionality, Optional return values and checked JSpecify annotations, and wrote down how values map at the boundaries. We also saw why the policy should be judged by incidents and data quality rather than purity, and why legacy schemas are protected with documentation and safe patterns instead of being rewritten.

What to say in the interviewAbout 80 seconds aloud

Open with

I would start by separating three ideas people mix up. SQL NULL means unknown or missing and brings three-valued logic. Java null is a reference to nothing, which causes NullPointerExceptions.

Then, if they want more

JSON has both an explicit null and a missing field, and in a PATCH they usually mean different things. The policy is mostly safe defaults. Schemas are NOT NULL unless the author documents what NULL means, enforced with migration linting. API contracts declare required fields and use nullable only when null is a real value.

In Java, methods that may not have a result return Optional, while code is annotated with JSpecify, NullMarked packages with Nullable exceptions, and a checker like NullAway in the build turns it into compile errors; Spring Framework 7 uses JSpecify too.

Then I'd define how values map at each boundary, so a missing value doesn't become zero or a deletion between layers. I'd measure success with null-related incidents and data quality checks, not with the percentage of annotated code.

And I'd accept that big legacy schemas stay nullable, documenting their NULL meanings and giving teams safe query patterns like NOT EXISTS and IS DISTINCT FROM.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 4, Spring Framework 7
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Tony Hoare called the null reference a billion-dollar mistake, and SQL NULL has its own...": https://scrivio-interview-library.netlify.app/databases/sql-fundamentals/97c66bca18/