{"record":{"id":"c9edb8b49eeef3f7","repo":"quarkusio/quarkus","slug":"cannot-cast-item-of-kind-kind-to-a-io-quarkus","errorCode":null,"errorMessage":"Cannot cast <item> of kind <kind> to a io.quarkus.redis.datasource.graph.GraphQueryResponseItem$ScalarItem","messagePattern":"Cannot cast <item> of kind <kind> to a io\\.quarkus\\.redis\\.datasource\\.graph\\.GraphQueryResponseItem\\$ScalarItem","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"extensions/redis-client/runtime/src/main/java/io/quarkus/redis/datasource/graph/GraphQueryResponseItem.java","lineNumber":21,"sourceCode":"import java.util.List;\n\npublic interface GraphQueryResponseItem {\n\n    enum Kind {\n        SCALAR,\n        NODE,\n        RELATION\n    }\n\n    Kind kind();\n\n    String name();\n\n    default ScalarItem asScalarItem() {\n        if (this instanceof ScalarItem) {\n            return (ScalarItem) this;\n        }\n        throw new ClassCastException(\"Cannot cast \" + this + \" of kind \" + kind() + \" to a \" + ScalarItem.class.getName());\n    }\n\n    default NodeItem asNodeItem() {\n        if (this instanceof NodeItem) {\n            return (NodeItem) this;\n        }\n        throw new ClassCastException(\"Cannot cast \" + this + \" of kind \" + kind() + \" to a \" + NodeItem.class.getName());\n    }\n\n    default RelationItem asRelationItem() {\n        if (this instanceof RelationItem) {\n            return (RelationItem) this;\n        }\n        throw new ClassCastException(\"Cannot cast \" + this + \" of kind \" + kind() + \" to a \" + RelationItem.class.getName());\n    }\n\n    interface ScalarItem extends GraphQueryResponseItem {\n","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/redis-client/runtime/src/main/java/io/quarkus/redis/datasource/graph/GraphQueryResponseItem.java#L3-L39","documentation":"GraphQueryResponseItem is a polymorphic result type for RedisGraph query results; each item is a SCALAR, NODE, or RELATION. The asScalarItem() convenience accessor throws a ClassCastException when the underlying item's kind() is not SCALAR, protecting you from an unsafe downcast. Call kind() first to know which concrete accessor is valid.","triggerScenarios":"Calling item.asScalarItem() on an item whose kind() is NODE or RELATION — typically when a Cypher query returns a heterogeneous list of results (e.g. 'MATCH (n)-[r]->(m) RETURN n, r, m') and code blindly calls asScalarItem() on every element.","commonSituations":"Iterating graph query result rows without checking kind(); column-order changes in the Cypher RETURN clause after a query edit; expecting a scalar like a count or string but the query actually returns nodes or edges.","solutions":["Check item.kind() == Kind.SCALAR before calling asScalarItem()","Switch over kind() and call the matching accessor: asScalarItem(), asNodeItem(), or asRelationItem()","Fix the Cypher query so the expected column really returns a scalar value","Catch ClassCastException around the accessor if kind-checking is impractical"],"exampleFix":"// before\nString s = result.get(0).asScalarItem().asString();\n// after\nGraphQueryResponseItem item = result.get(0);\nif (item.kind() == GraphQueryResponseItem.Kind.SCALAR) {\n    String s = item.asScalarItem().asString();\n} else {\n    throw new IllegalStateException(\"Expected scalar, got \" + item.kind());\n}","handlingStrategy":"type-guard","validationCode":"if (item.kind() != GraphQueryResponseItem.Kind.SCALAR) {\n    throw new IllegalStateException(\"Expected SCALAR but got \" + item.kind());\n}","typeGuard":"boolean isScalar(GraphQueryResponseItem item) {\n    return item.kind() == GraphQueryResponseItem.Kind.SCALAR;\n}","tryCatchPattern":"try {\n    ScalarItem s = item.asScalarItem();\n} catch (ClassCastException e) {\n    // fall back: inspect kind(), handle NODE/RELATION\n}","preventionTips":["Always branch on kind() before using as*Item accessors","Write one dispatch helper (switch over Kind) reused for all graph results","Keep Cypher RETURN clause and result-parsing code in sync; add tests for query shape"],"tags":["classcast","redis","graph","type-mismatch"],"backgroundTag":"class-cast-mismatch","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}