{"record":{"id":"edf3b33604fdce72","repo":"java-native-access/jna","slug":"unable-to-cast-to-cfarray-type-id-gettypeid","errorCode":null,"errorMessage":"Unable to cast to CFArray. Type ID: {getTypeID()}","messagePattern":"Unable to cast to CFArray\\. Type ID: (.+?)","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java","lineNumber":316,"sourceCode":"        }\n    }\n\n    /**\n     * A reference to an immutable {@code CFArray} object.\n     * <p>\n     * CFArray is “toll-free bridged” with its Cocoa Foundation counterpart,\n     * {@code NSArray}. Therefore, in a method where you see an {@code NSArray *}\n     * parameter, you can pass in a {@code CFArrayRef} .\n     */\n    class CFArrayRef extends CFTypeRef {\n        public CFArrayRef() {\n            super();\n        }\n\n        public CFArrayRef(Pointer p) {\n            super(p);\n            if (!isTypeID(ARRAY_TYPE_ID)) {\n                throw new ClassCastException(\"Unable to cast to CFArray. Type ID: \" + getTypeID());\n            }\n        }\n\n        /**\n         * Convenience method for {@link #CFArrayGetCount} on this object\n         *\n         * @return The number of values in this array.\n         */\n        public int getCount() {\n            return INSTANCE.CFArrayGetCount(this).intValue();\n        }\n\n        /**\n         * Convenience method for {@link #CFArrayGetValueAtIndex} on this object\n         *\n         * @param idx\n         *            The index of the value to retrieve.\n         * @return The value at the {@code idx} index.","sourceCodeStart":298,"sourceCodeEnd":334,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/mac/CoreFoundation.java#L298-L334","documentation":"CFArrayRef's Pointer constructor checks that the wrapped Core Foundation object's type id equals the CFArray type id and throws ClassCastException if not. This guards against narrowing an arbitrary CFTypeRef/Pointer into CFArrayRef.","triggerScenarios":"Constructing new CFArrayRef(pointer) from a polymorphic result such as a value read from a CFDictionary (e.g. valueForKey returning a CFString) or a NULL pointer whose type id is 0.","commonSituations":"Reading a dictionary entry expected to be a list but actually a scalar; iterating heterogeneous CFArray elements and mis-casting an element (arrays store CFTypeRefs, not CFArrayRefs); released/invalid pointers.","solutions":["Validate with new CFTypeRef(p).isTypeID(CoreFoundation.ARRAY_TYPE_ID) before constructing CFArrayRef.","Check for NULL pointers before casting (missing key/element).","Remember CFArray elements are CFTypeRefs — inspect each element's type id individually instead of assuming array-ness.","Fix the lookup path if the producer stores a scalar where an array was expected."],"exampleFix":"// before\nCFArrayRef arr = new CFArrayRef(value); // throws if value is a CFString\n// after\nCFTypeRef ref = new CFTypeRef(value);\nif (ref.isTypeID(CoreFoundation.ARRAY_TYPE_ID)) {\n    CFArrayRef arr = new CFArrayRef(value);\n} else {\n    // treat as scalar value\n}","handlingStrategy":"type-guard","validationCode":"if (value != null && new CFTypeRef(value).isTypeID(CoreFoundation.ARRAY_TYPE_ID)) {\n    CFArrayRef arr = new CFArrayRef(value);\n}","typeGuard":"boolean isCFArray(Pointer p) {\n    return p != null && new CFTypeRef(p).isTypeID(CoreFoundation.ARRAY_TYPE_ID);\n}","tryCatchPattern":"try {\n    CFArrayRef arr = new CFArrayRef(value);\n} catch (ClassCastException e) {\n    // scalar value where array expected; handle by type\n}","preventionTips":["Verify array-ness via isTypeID before constructing","Inspect each CFArray element's type id individually — elements are CFTypeRefs","Null-check results before casting","Dispatch on CFGetTypeID when a value can legitimately be multiple types"],"tags":["macos","corefoundation","classcastexception","native"],"backgroundTag":"type-mismatch","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}