Quick revision
The short answer
A bound method reference evaluates its receiver expression immediately, at the moment the reference is created, so config.getName()::trim calls getName() right away and, if it returns null, throws NullPointerException on that line. The lambda () -> config.getName().trim() only runs its body when get() is called, so nothing is evaluated earlier. The same rule means a bound reference keeps the receiver value it saw at creation, and does not notice later changes.
What to remember
In this answer, we saw that a bound method reference evaluates its receiver expression when the reference is created. config.getName()::trim calls getName() immediately and throws NullPointerException on that line if it returns null, through a null check javac inserts. The lambda () -> config.getName().trim() evaluates nothing until get() is called, so it fails later or not at all, with a more helpful message. The same rule makes a bound reference a snapshot of the receiver at creation, while the lambda reads the current value on each call, so we choose the form that matches what we mean.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A bound method reference evaluates its receiver expression immediately, at the moment the reference is created, so config.getName()::trim calls getName() right away and, if it returns null, throws NullPointerException on that line. The lambda () -> config.getName().trim() only runs its body when get() is called, so nothing is evaluated earlier. The same rule means a bound reference keeps the receiver value it saw at creation, and does not notice later changes.
Why this question is asked
Most of the time we treat a method reference as exactly the same thing as the lambda it replaces, and IDEs even offer to convert between them automatically. This question shows a case where they behave differently, and the difference surfaces as a NullPointerException on a line where nothing seems to be called. Interviewers ask it because it tests whether we know when each part of an expression is evaluated, which also explains stale values and lazy logging. In this answer, we will recap bound references, reproduce the exception, see why it is thrown at creation, compare the lambda, look at the stale-value side of the same rule, and decide which form to use.
1A Quick Recap of Bound Method References
A method reference written as expression::method is called bound, because the method will be called on one particular object, the receiver, which is bound to the reference. System.out::println and greeting::concat are bound references. This differs from an unbound reference like String::trim, where the receiver is supplied later as the first argument.
The Java Language Specification is precise about when that receiver is computed: when the method reference expression itself is evaluated, that is, when the line that creates the Supplier runs. If the receiver expression evaluates to null at that point, a NullPointerException is thrown immediately. Nothing is deferred to the call of get().
2Reproducing the Exception
Let us create a config whose name is not set, and build the Supplier both ways.
Notice where the trace points: at the line that creates s, through Objects.requireNonNull. To implement the specification's rule, javac evaluates the receiver expression, config.getName(), and inserts a null check on the result before handing it to the lambda factory (current compilers use Objects.requireNonNull; check your compiler for the exact form). The NullPointerException carries no helpful message, because it is thrown explicitly by that check rather than by the JVM failing to call trim().
3Why the Lambda Behaves Differently
The lambda () -> config.getName().trim() is a function body. Creating the lambda evaluates nothing inside the body; it only captures config, the reference to the Config object, which is not null. The call to getName() and the call to trim() happen each time get() runs.
6 messages in order. Play them, or step through them one at a time.
The reference evaluates its receiver at creation; the lambda evaluates its body at each call.
So the lambda can still fail, but at a different time, and with the JVM's helpful NullPointerException message that names the null expression. If get() is never called, for example because the Supplier is only used when debug logging is enabled, the lambda never fails at all.
4The Same Rule Gives a Snapshot, Not a Live View
Early evaluation has a second effect that causes quieter bugs. Because the receiver is computed once, the bound reference keeps that value forever, even if the source changes later.
The reference captured the String object that getName() returned at creation, so it trims that string forever. The lambda captured config and asks it for the name on every call. Neither is wrong, but they mean different things, and an IDE refactoring that turns one into the other can change behaviour silently. The same applies to System.out::println, which keeps writing to the stream that System.out referred to when the reference was created, even after System.setOut.
5Choosing Between the Two Forms
If we want the current value at each call, or the value might be null until later, we use the lambda. That is the typical case for lazy suppliers, such as messages passed to a logger that only builds them when the level is enabled, or defaults passed to Optional.orElseGet.
If we want a snapshot, the bound reference works, but it is clearer to make the snapshot explicit with a local variable, and to handle the null case on purpose, so that the next reader does not have to know the evaluation rule.
Notice that String::trim inside map is an unbound reference, with the receiver supplied by the Optional, so it never evaluates anything early.
6Follow-up questions
Does an unbound reference like String::trim have the same problem?
No. An unbound reference has no receiver expression to evaluate; the receiver is the first argument supplied when the functional method is called. If that argument is null, the NullPointerException happens at call time, like a lambda.
Why did the language designers choose early evaluation?
Because a method reference is an expression, and Java evaluates expressions where they appear. Evaluating the receiver once also matches how most people read obj::method, as a function attached to that object, and avoids re-running a possibly expensive or side-effecting expression on every call.
Can static analysis catch this?
Some tools warn when a bound method reference's receiver may be null, using nullability annotations such as @Nullable on getName(). Without annotations, the safest habit is to use bound references only on receivers that cannot be null, such as fields set in a constructor or this.
7Conclusion
In this answer, we saw that a bound method reference evaluates its receiver expression when the reference is created. config.getName()::trim calls getName() immediately and throws NullPointerException on that line if it returns null, through a null check javac inserts. The lambda () -> config.getName().trim() evaluates nothing until get() is called, so it fails later or not at all, with a more helpful message. The same rule makes a bound reference a snapshot of the receiver at creation, while the lambda reads the current value on each call, so we choose the form that matches what we mean.
Open with
The difference is when the receiver expression is evaluated. config.getName()::trim is a bound method reference, and the language specification says the receiver of a bound reference is evaluated when the reference itself is evaluated, which is the line that creates the Supplier.
Then, if they want more
So getName() is called right away, and if it returns null the reference cannot be created and a NullPointerException is thrown on that line. javac implements this with a null check on the receiver, so the stack trace goes through Objects.requireNonNull and has no helpful message.
The lambda () -> config.getName().trim() is a function body; creating it only captures config, which is not null, and getName and trim run each time get() is called. So the lambda fails only when it is used, with the JVM's helpful message, or never if get is never called.
The same rule has a second effect: a bound reference is a snapshot. If the name changes after I create the reference, the reference still trims the old string, while the lambda reads the current name every time.
So for lazy values or values that may be null until later I use the lambda, and if I really want a snapshot I make it explicit with a local variable and a deliberate null check.
About this answer
- Last updated
- 12 November 2026
- 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.
- Official documentation
- java.util.stream, package documentation, java.util.function, package documentation
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 "This code throws NullPointerException at the line where the method reference is created...": https://scrivio-interview-library.netlify.app/java/lambdas/6805423993/