{"record":{"id":"dd9088a33550d0e3","repo":"hibernate/hibernate-orm","slug":"result-class-is-null","errorCode":null,"errorMessage":"Result class is null","messagePattern":"Result class is null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java","lineNumber":2120,"sourceCode":"\n\n\n\n\n\n\n\n\n\n\t// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\t// Named Query\n\t@Override\n\t@Nonnull\n\tpublic <R> SelectionQueryImplementor<R> createNamedQuery(@Nonnull String name, @Nonnull Class<R> resultClass) {\n\t\tchecksBeforeQueryCreation();\n\t\t//noinspection ConstantValue\n\t\tif ( resultClass == null ) {\n\t\t\tthrow new IllegalArgumentException( \"Result class is null\" );\n\t\t}\n\t\ttry {\n\t\t\tfinal QueryImplementor<R> query = buildNamedQuery( name,\n\t\t\t\t\tmemento -> createSqmQueryImplementor( resultClass, memento ),\n\t\t\t\t\tmemento -> createNativeQueryImplementor( resultClass, memento ) );\n\t\t\tif ( query instanceof SelectionQueryImplementor<R> selectionQuery ) {\n\t\t\t\treturn selectionQuery;\n\t\t\t}\n\t\t\telse {\n\t\t\t\t// JPA implies (though is not very explicit) that this should lead\n\t\t\t\t// to an IllegalArgumentException.  Yuck, but...\n\t\t\t\tvar msg = \"Named query is not a selection query : \" + name;\n\t\t\t\tvar iae = new IllegalArgumentException( msg );\n\t\t\t\tiae.addSuppressed( new IllegalSelectQueryException( msg, query.getQueryString() ) );\n\t\t\t\tthrow iae;\n\t\t\t}\n\t\t}\n\t\tcatch (RuntimeException e) {","sourceCodeStart":2102,"sourceCodeEnd":2138,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java#L2102-L2138","documentation":"createNamedQuery(name, resultClass) declares the result class @Nonnull and fails fast with IllegalArgumentException('Result class is null') before any name lookup. The typed overload cannot mean 'any type', so callers must either pass a concrete Class or use the single-argument createNamedQuery(name).","triggerScenarios":"Passing null explicitly, or a generic Class<T> variable that is null at runtime: unchecked casts of never-set fields, Optional.orElse(null), Map.get(key) with a missing key, or nullable Kotlin values crossing into Java.","commonSituations":"Generic repository helpers plumbing an optional type parameter that some callers never set; refactors that removed the class argument but kept the two-arg overload; DI/config-supplied Class fields left null by a misconfigured bean.","solutions":["Use the untyped createNamedQuery(name) when no concrete result class applies.","Fix the generic plumbing so a real Class<T> reaches the call; requireNonNull with a descriptive message near the source.","Validate optional type parameters at construction time of the helper rather than at query creation."],"exampleFix":"// before\npublic <T> List<T> byName(String name, Class<T> type) {\n    return em.createNamedQuery(name, type).getResultList(); // type null -> throws\n}\n// after\npublic <T> List<T> byName(String name, Class<T> type) {\n    return type != null ? em.createNamedQuery(name, type).getResultList()\n                        : em.createNamedQuery(name).getResultList();\n}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static <T> Class<T> requireResultClass(@Nullable Class<T> type) {\n    return Objects.requireNonNull(type, \"resultClass must not be null; use createNamedQuery(name)\");\n}\n\n// usage\nreturn em.createNamedQuery(name, requireResultClass(type)).getResultList();","tryCatchPattern":null,"preventionTips":["Avoid Optional.orElse(null) and unchecked casts for Class arguments","Prefer the single-argument createNamedQuery(name) when type is genuinely optional","Validate optional type parameters when the helper object is constructed"],"tags":["hibernate","named-query","null-check","argument-validation"],"backgroundTag":"null-argument","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}