{"record":{"id":"f43f5813d23558cc","repo":"java-native-access/jna","slug":"invalid-structure-field-in-getclass-field-name-name-type-e","errorCode":null,"errorMessage":"Invalid Structure field in \" + getClass() + \", field name '\" + name + \"' (\" + type + \"): \" + e.getMessage()","messagePattern":"Invalid Structure field in \" \\+ getClass\\(\\) \\+ \", field name '\" \\+ name \\+ \"' \\(\" \\+ type \\+ \"\\): \" \\+ e\\.getMessage\\(\\)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Structure.java","lineNumber":1299,"sourceCode":"\n    private void validateField(String name, Class<?> type) {\n        if (typeMapper != null) {\n            ToNativeConverter toNative = typeMapper.getToNativeConverter(type);\n            if (toNative != null) {\n                validateField(name, toNative.nativeType());\n                return;\n            }\n        }\n        if (type.isArray()) {\n            validateField(name, type.getComponentType());\n        }\n        else {\n            try {\n                getNativeSize(type);\n            }\n            catch(IllegalArgumentException e) {\n                String msg = \"Invalid Structure field in \" + getClass() + \", field name '\" + name + \"' (\" + type + \"): \" + e.getMessage();\n                throw new IllegalArgumentException(msg, e);\n            }\n        }\n    }\n\n    /** ensure all fields are of valid type. */\n    private void validateFields() {\n        // Try to read the value under the read lock\n        cacheStructureLock.readLock().lock();\n        try {\n            if (validationMap.containsKey(getClass())) {\n                return; // Return because this Structure has already been validated\n            }\n        } finally {\n            cacheStructureLock.readLock().unlock();\n        }\n\n        // If not found, perform validation and update the cache under the write lock\n        cacheStructureLock.writeLock().lock();","sourceCodeStart":1281,"sourceCodeEnd":1317,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Structure.java#L1281-L1317","documentation":"JNA validates every field of a Structure subclass by asking the layout engine for its native size. If a field's Java type has no native mapping (unknown class, un-sizeable type), IllegalArgumentException is thrown and re-wrapped with the Structure class, field name and type. It means the structure cannot be laid out in native memory.","triggerScenarios":"Defining a Structure field whose type has no registered native size: an unknown object type, a type not covered by the TypeMapper or a nested type that fails getNativeSize(type) during field validation (validateFields / calculateSize).","commonSituations":"Adding a custom class field to a Structure without a TypeMapper; typos in nested structure types; using types only valid after a type mapper is set; version changes where a previously supported type lost its mapping.","solutions":["Change the field to a JNA-supported type (primitive, wrapper, Pointer, String, WString, Buffer, arrays, nested Structure/Structure.ByReference, Callback)","Register a TypeMapper (or use @Structure.FieldOrder-compatible custom marshalling) covering the custom field type","If the type uses NativeMapped, verify the class implements nativeSize()/toNative()/fromNative() correctly","Read the wrapped cause (e.getMessage) which states which type could not be sized"],"exampleFix":"// before\nclass Cfg extends Structure {\n    public MyCustomType cfg; // no native mapping\n}\n// after\nclass Cfg extends Structure {\n    public Pointer cfg; // or use a TypeMapper/NativeMapped for MyCustomType\n}","handlingStrategy":"type-guard","validationCode":"for (Field f : Cfg.class.getFields()) {\n    Class<?> t = f.getType();\n    boolean ok = t.isPrimitive() || Number.class.isAssignableFrom(t) || t == Pointer.class\n        || t == String.class || NativeMapped.class.isAssignableFrom(t)\n        || Structure.class.isAssignableFrom(t) || t.isArray() || Callback.class.isAssignableFrom(t);\n    if (!ok) throw new IllegalStateException(\"Field not natively mappable: \" + f);\n}","typeGuard":"static boolean isMappable(Class<?> t) {\n    return t.isPrimitive() || Number.class.isAssignableFrom(t) || t == Pointer.class\n        || t == String.class || NativeMapped.class.isAssignableFrom(t)\n        || (Structure.class.isAssignableFrom(t) && t != Structure.class)\n        || (t.isArray() && t.getComponentType() != Structure.class)\n        || (Callback.class.isAssignableFrom(t) && t.isInterface());\n}","tryCatchPattern":"try {\n    cfg.size();\n} catch (IllegalArgumentException e) {\n    if (e.getCause() != null && e.getMessage().contains(\"Invalid Structure field\")) {\n        throw new IllegalStateException(\"Fix unmapped structure field: \" + e.getMessage(), e);\n    }\n    throw e;\n}","preventionTips":["Only use documented JNA-native field types in Structures","Implement NativeMapped or register a TypeMapper for custom types","Write a unit test calling size() on every Structure to validate layout at build time","Keep nested types concrete and supported"],"tags":["jna","structure","native","type-mapping"],"backgroundTag":"type-mismatch","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}