{"record":{"id":"f1aa6da52dba4d70","repo":"hibernate/hibernate-orm","slug":"attempted-to-refresh-null","errorCode":null,"errorMessage":"Attempted to refresh null","messagePattern":"Attempted to refresh null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java","lineNumber":61,"sourceCode":" */\npublic class DefaultRefreshEventListener implements RefreshEventListener {\n\n\t@Override\n\tpublic void onRefresh(@Nonnull RefreshEvent event) {\n\t\tonRefresh( event, RefreshContext.create() );\n\t}\n\n\t/**\n\t * Handle the given refresh event.\n\t *\n\t * @param event The refresh event to be handled.\n\t */\n\t@Override\n\tpublic void onRefresh(@Nonnull RefreshEvent event, @Nonnull RefreshContext refreshedAlready) {\n\t\tfinal Object object = event.getObject();\n\t\t//noinspection ConstantValue\n\t\tif ( object == null ) {\n\t\t\tthrow new NullPointerException( \"Attempted to refresh null\" );\n\t\t}\n\t\tfinal var source = event.getEventSource();\n\t\tif ( isUninitialized( object, source ) ) {\n\t\t\thandleUninitializedProxy( event, refreshedAlready );\n\t\t}\n\t\telse {\n\t\t\tfinal Object entity = forceInitialize( object, source );\n\t\t\tif ( refreshedAlready.add( entity ) ) {\n\t\t\t\trefresh( event, refreshedAlready, entity );\n\t\t\t}\n\t\t\telse {\n\t\t\t\tEVENT_LISTENER_LOGGER.alreadyRefreshed();\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate static void handleUninitializedProxy(@Nonnull RefreshEvent event, @Nonnull RefreshContext refreshedAlready) {\n\t\tfinal var source = event.getEventSource();","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultRefreshEventListener.java#L43-L79","documentation":"NullPointerException thrown by DefaultRefreshEventListener.onRefresh when event.getObject() is null, i.e. session.refresh(null) / EntityManager.refresh(null). The refresh pipeline needs a real managed instance to reload from the database; the listener enforces the @Nonnull contract with an explicit null check. A null here is almost always a bug in the caller's data flow (a lookup that returned null was passed on).","triggerScenarios":"session.refresh(x) or em.refresh(x) where x is null — typically the result of em.find(Entity.class, id) for a non-existent row (find returns null) being forwarded to refresh unconditionally; nullable relation fields dereferenced into refresh helpers.","commonSituations":"Optional-entity flows where a missing row silently becomes null; batch helper methods like refreshAll(List) invoked with a null list; test code or mocks passing null; JPA spec expects IllegalArgumentException for refresh(null), so ported code that relied on that behavior hits the NPE wording instead.","solutions":["Null-check (or wrap in Optional) before calling refresh, and treat null as 'row not found' business logic instead of refreshable state","Trace the null to its origin — usually a find() miss caused by a wrong id, a not-yet-committed insert from another transaction, or a deleted row","Use Optional-style chaining so a null can never reach the session API"],"exampleFix":"// before\nOrder o = em.find(Order.class, id);\nem.refresh(o); // NPE: Attempted to refresh null when id does not exist\n\n// after\nOptional.ofNullable(em.find(Order.class, id)).ifPresent(em::refresh);","handlingStrategy":"validation","validationCode":"if (order != null) {\n    session.refresh(order);\n}\n// or: Optional.ofNullable(find(id)).ifPresent(em::refresh);","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Treat a null from find() as not-found business state; never forward it to session APIs","Wrap refresh calls in Optional chains or guard clauses in shared helper methods","Enable @Nonnull-by-default annotations/NullAway in the codebase to catch nullable flows at compile time"],"tags":["hibernate","null-check","refresh","session-api","orm"],"backgroundTag":"null-argument-to-orm-session","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}