Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

An @Around advice logs the method time, but every advised method now returns null. What is the bug?

Java
@Around("@annotation(Timed)")
public void time(ProceedingJoinPoint pjp) throws Throwable {
long t = System.nanoTime();
pjp.proceed();
log(t);
}

The advice is declared void and calls pjp.proceed() without keeping its result, so the real return value is thrown away and the proxy hands null back to every caller. The fix is to declare the advice as returning Object and return the result of pjp.proceed(). The time should be logged in a finally block so it is recorded even when the method throws, and the exception must be allowed to propagate rather than being caught and swallowed.

Why this question is asked

This is a small bug with a large blast radius: one shared timing aspect, added for good reasons, makes every annotated method in the service return null. It is also easy to write, because a void advice method compiles without any warning. Interviewers ask it because the answer shows whether we understand that @Around advice really replaces the method call, and therefore owns its result and its exceptions. In this answer, we will recap how @Around works, see exactly where the value disappears, write the fixed advice, and then handle exceptions and primitive return types correctly.

1A Quick Recap of @Around Advice

Spring applies aspects through a proxy, a generated object in front of our bean. When a caller invokes an advised method, the proxy does not call the method directly. It calls our @Around advice and passes it a ProceedingJoinPoint, an object that represents the pending method call. Calling pjp.proceed() runs the real method (or the next advice in the chain) and returns whatever the method returned.

Whatever our advice method returns is what the proxy returns to the caller. That is the key point. @Around advice is not watching the call from the side; it is the call, as far as the caller is concerned.

2Where the Return Value Disappears

Let us follow a call to an advised method that returns an order.

Java
@Around("@annotation(Timed)")
public void time(ProceedingJoinPoint pjp) throws Throwable {
long t = System.nanoTime();
pjp.proceed(); // the real result is returned here... and ignored
log(t);
} // void: nothing goes back to the proxy
@Timed
public Order findOrder(long id) { return repo.findById(id).orElseThrow(); }
Order o = orderService.findOrder(7);
System.out.println(o); // null, although the database row exists
Step throughThe result is lost inside the advice
CallerProxytime() advicefindOrder()
findOrder(7)
time(pjp)
pjp.proceed()
Order 7
result ignored
void: nothing
null

7 messages in order. Play them, or step through them one at a time.

The advice stands between the caller and the method, so whatever it returns is the result.

The real findOrder() runs and returns an Order to proceed(). The advice ignores that value and returns nothing. The proxy then has to give the caller something of type Order, and the only thing it has is null. So every @Timed method returns null, whatever it computed.

It gets worse for primitive return types. A method declared to return int or boolean cannot return null, so for those the proxy throws an AopInvocationException with a message saying that a null return value from the advice does not match the primitive return type. One aspect can therefore produce nulls in some methods and exceptions in others, which makes the bug look like several unrelated problems.

3The Fix: Return What proceed() Returns

The advice must be declared to return Object and must return the result of proceed(). Object works for every advised method: the proxy casts it back to the method's real return type, and unboxes it for primitives.

Java
@Around("@annotation(Timed)")
public Object time(ProceedingJoinPoint pjp) throws Throwable {
long t = System.nanoTime();
try {
return pjp.proceed(); // the real result goes back to the caller
} finally {
log(pjp.getSignature().toShortString(), System.nanoTime() - t); // logged on success and failure
}
}
Order o = orderService.findOrder(7);
System.out.println(o.id()); // 7
// log: OrderService.findOrder(..) took 1843000 ns

Notice two changes. The return type is Object and the advice returns proceed()'s value directly. And the timing moved into a finally block, so it runs whether the method returns or throws. In the original version, if findOrder() threw, log(t) would never run, so the slowest and most interesting calls, the failing ones, would have no timing at all.

4Let Exceptions Propagate

A tempting but wrong variation catches the exception to log it.

Java
// Wrong: swallows the failure
try {
return pjp.proceed();
} catch (Throwable ex) {
log.error("failed", ex);
return null; // caller sees null instead of the exception
}
// Right: observe, then rethrow (or simply use finally, as above)
try {
return pjp.proceed();
} catch (Throwable ex) {
log.warn("{} failed: {}", pjp.getSignature().toShortString(), ex.toString());
throw ex; // the caller, and any @Transactional rollback, still see it
}

Swallowing the exception has the same effect as the original bug and more: the caller gets null instead of an error, and if a @Transactional advice is further out in the chain, it never sees the exception and commits a transaction that should have rolled back. A timing aspect should never change the outcome of the call it is measuring. Declaring throws Throwable on the advice method, as proceed() does, lets checked and unchecked exceptions pass through unchanged.

5A Checklist for Any @Around Advice

The same rules apply to every @Around advice, not just timing. It declares Object as its return type. It calls proceed() exactly once, unless skipping or retrying is its purpose. It returns proceed()'s result, unless changing the result is its purpose. It uses finally for anything that must happen in all cases. And it rethrows exceptions, unless translating them is its purpose. When none of those purposes applies, a simpler advice type may be the better choice, and for timing in particular Micrometer's @Timed or @Observed annotations already do this correctly and publish the result as metrics.

6Follow-up questions

Why does a void advice compile at all?

Because Spring cannot tell at compile time which methods the pointcut will match. A void @Around is valid when it only advises void methods, so neither the compiler nor Spring rejects it. It becomes a bug when the pointcut matches methods that return values.

Is System.nanoTime() the right clock here?

Yes. nanoTime() measures elapsed time and is not affected by wall-clock changes, which is what we want for durations. currentTimeMillis() can jump when the system clock is adjusted.

What would the advice return for a void method?

proceed() returns null for a void method, and the advice returns that null, which the proxy ignores. So the same Object-returning advice works for void and non-void methods alike.

7Conclusion

In this answer, we saw that @Around advice stands between the caller and the method, so its return value is the method's result as far as the caller knows. Declaring the advice void and ignoring proceed()'s result makes every advised method return null, or throw AopInvocationException for primitives. The fix is to return Object and return proceed()'s result, log the time in finally so failures are measured too, and let exceptions propagate so callers and transactions see them.

What to say in the interviewAbout 90 seconds aloud

Open with

The bug is that the advice is declared void and ignores what proceed returns. With @Around, the advice replaces the call from the caller's point of view: the proxy calls the advice, and whatever the advice returns is what the caller gets.

Then, if they want more

Here the real method runs and returns its value to proceed, the advice throws that value away, and the proxy has nothing to return, so every advised method returns null. For methods with primitive return types it is even louder, the proxy throws an AopInvocationException because null cannot be converted to an int or boolean.

The fix is to declare the advice as returning Object and return pjp.proceed() directly. I would also move the logging into a finally block, because in the original code an exception from the method skips the log line, so the failing calls, which are often the slow ones, never get timed.

And the advice must not swallow exceptions: if it catches anything it should rethrow, otherwise the caller gets null instead of an error and an outer @Transactional advice would commit instead of rolling back. In general, an @Around advice should return Object, call proceed once, return its result, use finally for cleanup and rethrow. For plain timing, Micrometer's @Timed or @Observed already does all of that.

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.
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 "An @Around advice logs the method time, but every advised method now returns null. What...": https://scrivio-interview-library.netlify.app/spring-boot/aop/14308ff5b2/