{"record":{"id":"72cba85df1286e57","repo":"hibernate/hibernate-orm","slug":"attempted-to-lock-null","errorCode":null,"errorMessage":"Attempted to lock null","messagePattern":"Attempted to lock null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLockEventListener.java","lineNumber":36,"sourceCode":" * Defines the default lock event listeners used by hibernate to lock entities\n * in response to generated lock events.\n *\n * @author Steve Ebersole\n */\npublic class DefaultLockEventListener implements LockEventListener {\n\n\t/**\n\t * Handle the given lock event.\n\t *\n\t * @param event The lock event to be handled.\n\t */\n\t@Override\n\tpublic void onLock(@Nonnull LockEvent event) {\n\n\t\tfinal Object instance = event.getObject();\n\t\t//noinspection ConstantValue\n\t\tif ( instance == null ) {\n\t\t\tthrow new NullPointerException( \"Attempted to lock null\" );\n\t\t}\n\n\t\tfinal var lockMode = event.getLockMode();\n\t\tif ( lockMode == LockMode.WRITE || lockMode == LockMode.UPGRADE_SKIPLOCKED ) {\n\t\t\tthrow new IllegalArgumentException( \"Invalid lock mode '\" + lockMode + \"' passed to 'lock()'\" );\n\t\t}\n\n\t\tfinal var source = event.getSession();\n\t\tfinal Object entity = forceInitialize( instance, source );\n\t\t//TODO: if instance was an uninitialized proxy, this is inefficient,\n\t\t//      resulting in two SQL selects\n\n\t\tfinal var entry = source.getPersistenceContextInternal().getEntry( entity );\n\t\tif ( entry == null && instance == entity ) {\n\t\t\tthrow new DetachedObjectException( \"Given entity is not associated with the persistence context\" );\n\t\t}\n\t\tassert entry != null;\n","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLockEventListener.java#L18-L54","documentation":"Error \"Attempted to lock null\" thrown in hibernate/hibernate-orm.","triggerScenarios":"Thrown when a caller passes a null Attempted to lock null to the API guarded in DefaultLockEventListener.java:36.","commonSituations":"Typical situations: calling the method with an uninitialized variable; a lookup that returned null being passed straight through; missing null-checks in application code before invoking Hibernate.","solutions":["Check the entity reference for null before calling lock().","Guard with Optional or an explicit null check before invoking Session.lock()."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}