Scrivio Interview library
CodingIntermediate6 min read · updated 12 November 2026

Given a package annotated @NullMarked, which of these lines would a nullness checker such as NullAway flag, and why?

Java
String name(User u) {
return u.nickname().trim();
}

where nickname() is declared as '@Nullable String nickname()'.

NullAway flags u.nickname().trim(), because nickname() is declared @Nullable and its result is dereferenced without a check. The parameter u is fine to dereference, since in a @NullMarked package an unannotated parameter is non-null. We fix the line with a null check, with Objects.requireNonNullElse, or by making the return type @Nullable, and NullAway will in turn flag any caller that passes null for u.

Why this question is asked

Reading code the way a nullness checker reads it is a useful skill: it is the same reasoning we do in our heads when we review code, made exact. Interviewers give this small example to see whether we understand what @NullMarked changes, where the real risk is, and how each fix shifts responsibility to someone else. In this answer, we will recap the rules a checker follows, walk through the line one dereference at a time, look at the error NullAway reports, try three fixes, and then see what happens at the call sites.

1A Quick Recap of the Rules

A nullness checker such as NullAway gives every type in our code one of two states: nullable or non-null. In a package annotated @NullMarked, from JSpecify, a type without an annotation is non-null, and a type annotated @Nullable may be null. The checker then enforces two simple rules. We must not dereference a nullable value, meaning call a method on it or read a field, unless we have proved it is not null on that path. And we must not pass, assign or return a nullable value where a non-null one is expected.

Proving a value is not null is usually done with an ordinary if check, which the checker understands: inside if (x != null) { ... }, x counts as non-null. This is called flow-sensitive analysis, because the type of the same variable changes along the flow of the code.

2Walking Through the Line

Here is the code from the question, with the declaration of nickname() and the package annotation.

Java
// package-info.java
@NullMarked
package com.example.users;
// User.java
public interface User {
@Nullable String nickname(); // may return null
}
// Names.java
class Names {
String name(User u) { // u: non-null by default, return type: non-null by default
return u.nickname().trim(); // u.nickname(): fine, u is non-null
// .trim(): flagged, the result of nickname() may be null
}
}
Step throughHow the checker reads u.nickname().trim()
  1. Dereference uu is unannotated in a @NullMarked package
  2. Is u non-null?
    YesYes: allowedNoNo: would be flagged
  3. Call nickname()declared @Nullable String
  4. Dereference the result with trim()no null check on this path
  5. Proved non-null here?
    YesAllowedNoFlagged: dereferenced expression is @Nullable

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

Only the second dereference is a problem.

The expression has two dereferences. The first is u.nickname(): we call a method on u. Because the package is @NullMarked and u has no annotation, u is non-null, so this is fine, and the checker does not ask us to test it. The second is .trim() on the result of nickname(). That result is declared @Nullable, and nothing on this path proved it is not null, so this is exactly the dereference the checker reports.

3What NullAway Reports

NullAway runs as an Error Prone plugin during compilation, so the problem appears as a compile error in the normal build. The message names the expression and the reason.

Java
Names.java:4: error: [NullAway] dereferenced expression u.nickname() is @Nullable
return u.nickname().trim();
^

The exact wording can differ between NullAway versions, so check your version, but the idea is the same: it points at the dereference of the nullable expression, not at the parameter. Notice that the error tells us where the risk is, not how to fix it, because the right fix depends on what the method should mean when there is no nickname.

4Three Ways to Fix It

The first fix is an explicit null check. We read the value into a local variable so that the check and the use refer to the same value, then decide what to return when it is missing.

Java
// Fix 1: check for null
String name(User u) {
String nick = u.nickname();
if (nick == null) {
return "anonymous"; // our choice for a missing nickname
}
return nick.trim(); // fine: nick is non-null on this path
}
// Fix 2: a default value in one call
String name(User u) {
return Objects.requireNonNullElse(u.nickname(), "anonymous").trim(); // non-null result
}
// Fix 3: admit that the result can be missing
@Nullable String name(User u) {
String nick = u.nickname();
return nick == null ? null : nick.trim(); // fine: the return type is now @Nullable
}

Notice why fix 1 uses a local variable. If we wrote if (u.nickname() != null) return u.nickname().trim(); a checker cannot be sure the second call returns the same thing, and by default NullAway does not assume methods are pure, so it may still complain. Fix 2 uses Objects.requireNonNullElse, which returns the first argument if it is not null and otherwise the second, and NullAway knows its result is non-null. Fix 3 does not solve the problem; it passes it on: now every caller of name() receives a @Nullable String and must handle it. That is the honest choice when there really is no good default. Using Optional is a fourth option some teams prefer for return values.

5What Happens at the Call Sites

The parameter u is non-null by default, so name() may dereference it without checking. The other half of that contract is enforced where name() is called: passing null, or passing a value that may be null, is an error at the call site.

Java
void greet(@Nullable User maybe, User sure) {
names.name(sure); // fine
names.name(null); // flagged: passing @Nullable parameter 'null' where @NonNull is required
names.name(maybe); // flagged: 'maybe' may be null
if (maybe != null) {
names.name(maybe); // fine: proved non-null on this path
}
}

This is the key idea behind null-marked code: each method trusts its non-null parameters, and the checker makes every caller keep that promise. So the null checks move to the one place where null can actually appear, instead of being repeated defensively in every method.

6Follow-up questions

Why does NullAway not complain about dereferencing u?

Because inside a @NullMarked scope an unannotated parameter is non-null. NullAway checks that callers never pass null, so inside the method u can be used directly. If we wanted u to accept null, we would write @Nullable User u, and then NullAway would flag u.nickname() as well.

What if User comes from a library that is not annotated?

Then nickname() has unspecified nullness and NullAway, by default, treats it as non-null to avoid noise, so it would not flag trim(). That is why annotated libraries, such as Spring Framework 7, matter, and why some teams add stub annotations or library models for important unannotated libraries.

Would Optional be a better return type for nickname()?

It is a reasonable design for return values, because the caller is forced to deal with absence through the API. But Optional adds an allocation and does not work well for fields or parameters. With JSpecify and a checker, @Nullable on the return type gives the same compile-time safety with plain types, so both styles are defensible; consistency within a code base matters most.

7Conclusion

In this answer, we read the line u.nickname().trim() the way NullAway does. Dereferencing u is fine because unannotated parameters are non-null in a @NullMarked package, but calling trim() on the @Nullable result of nickname() is flagged. We fixed it with a null check, with Objects.requireNonNullElse, or by making the return type @Nullable, and we saw that the checker enforces the non-null parameter at every call site.

What to say in the interviewAbout 80 seconds aloud

Open with

The checker would flag the call to trim, not the call to nickname. Inside a NullMarked package, every unannotated type is non-null, so the parameter u is non-null and calling u.nickname() is fine; NullAway does not ask me to check it.

Then, if they want more

But nickname() is declared with @Nullable, so its result may be null, and calling trim on it without a check is a dereference of a nullable expression, which NullAway reports as a compile error. To fix it, I first decide what the method should mean when there is no nickname.

If there is a sensible default, I write a null check on a local variable, or use Objects.requireNonNullElse with the default and then trim. I use a local variable rather than calling nickname twice, because the checker cannot assume two calls return the same value.

If there is no good default, I make the return type of name @Nullable and handle the missing value, which moves the responsibility to the callers. And the contract works in both directions: because u is non-null, any caller passing null, or a variable that might be null without checking it, is flagged at the call site.

That is the point of null-marked code: the checks happen once, where null can really appear.

About this answer

Last updated
12 November 2026
Versions it names
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 "Given a package annotated @NullMarked, which of these lines would a nullness checker...": https://scrivio-interview-library.netlify.app/java/code-quality-tools/cd501a696c/