{"record":{"id":"05ad9be4dc19cf22","repo":"hibernate/hibernate-orm","slug":"bean-instance-cannot-be-null","errorCode":null,"errorMessage":"Bean instance cannot be null","messagePattern":"Bean instance cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/resource/beans/spi/ProvidedInstanceManagedBeanImpl.java","lineNumber":20,"sourceCode":" * SPDX-License-Identifier: Apache-2.0\n * Copyright Red Hat Inc. and Hibernate Authors\n */\npackage org.hibernate.resource.beans.spi;\n\nimport org.hibernate.internal.util.ReflectHelper;\n\n/**\n * {@link ManagedBean} implementation for cases where we have been handed an actual\n * instance to use.\n *\n * @author Steve Ebersole\n */\npublic class ProvidedInstanceManagedBeanImpl<T> implements ManagedBean<T> {\n\tprivate final T instance;\n\n\tpublic ProvidedInstanceManagedBeanImpl(T instance) {\n\t\tif ( instance == null ) {\n\t\t\tthrow new IllegalArgumentException( \"Bean instance cannot be null\" );\n\t\t}\n\t\tthis.instance = instance;\n\t}\n\n\t@Override\n\tpublic Class<T> getBeanClass() {\n\t\treturn ReflectHelper.getClass( instance );\n\t}\n\n\t@Override\n\tpublic T getBeanInstance() {\n\t\treturn instance;\n\t}\n}\n","sourceCodeStart":2,"sourceCodeEnd":35,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/resource/beans/spi/ProvidedInstanceManagedBeanImpl.java#L2-L35","documentation":"ProvidedInstanceManagedBeanImpl is the ManagedBean wrapper Hibernate uses when an actual bean instance is handed to it directly. Every ManagedBean must be able to produce an instance, so the constructor rejects null with IllegalArgumentException('Bean instance cannot be null') - the caller supplied no instance where one is mandatory.","triggerScenarios":"A custom BeanContainer or BeanInstanceProducer returns null instead of throwing when a bean cannot be looked up, and that null is wrapped into a ManagedBean; or configuration code passes a null bean instance into the managed-bean registry.","commonSituations":"Hand-written producers whose lookup silently returns null for missing beans; race conditions where a lookup happens before registration and yields null; copy-pasted configuration referencing an unset field.","solutions":["Never return null from a producer: throw a descriptive exception naming the missing bean type instead.","Null-check at the boundary that hands instances to Hibernate and fail fast with context.","Initialize/verify the instance field before building the SessionFactory."],"exampleFix":"// before\npublic <B> B produceBeanInstance(Class<B> type) {\n    return registry.get(type); // returns null when absent -> IllegalArgumentException\n}\n\n// after\npublic <B> B produceBeanInstance(Class<B> type) {\n    B bean = registry.get(type);\n    if (bean == null) throw new IllegalStateException(\"No bean registered for \" + type.getName());\n    return bean;\n}","handlingStrategy":"validation","validationCode":"// producers must never hand null to Hibernate\npublic <B> ManagedBean<B> bean(Class<B> type) {\n    B instance = lookup(type);\n    java.util.Objects.requireNonNull(instance, () -> \"No instance for \" + type.getName());\n    return new ProvidedInstanceManagedBeanImpl<>(instance);\n}","typeGuard":null,"tryCatchPattern":"try {\n    return new ProvidedInstanceManagedBeanImpl<>(producer.produceBeanInstance(type));\n} catch (IllegalArgumentException e) {\n    // 'Bean instance cannot be null': producer returned null; fail with bean context\n    throw new BeanLookupException(\"Producer returned null for \" + type.getName(), e);\n}","preventionTips":["Throw descriptive exceptions from lookups instead of returning null.","RequireNonNull at every producer boundary.","Initialize the instance registry before SessionFactory construction."],"tags":["hibernate","validation","null-check","bean-container"],"backgroundTag":"null-configuration-value","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}