{"record":{"id":"4be64960d0223208","repo":"java-native-access/jna","slug":"structure-getclass-has-unknown-or-zero-size-ensure-all","errorCode":null,"errorMessage":"Structure \" + getClass() + \" has unknown or zero size (ensure all fields are public)\"","messagePattern":"Structure \" \\+ getClass\\(\\) \\+ \" has unknown or zero size \\(ensure all fields are public\\)\"","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Structure.java","lineNumber":1471,"sourceCode":"                structField.offset = calculatedSize;\n                calculatedSize += structField.size;\n            }\n\n            // Save the field in our list\n            info.fields.put(structField.name, structField);\n        }\n\n        if (calculatedSize > 0) {\n            int size = addPadding(calculatedSize, info.alignment);\n            // Update native FFI type information, if needed\n            if (this instanceof ByValue && !avoidFFIType) {\n                getTypeInfo();\n            }\n            info.size = size;\n            return info;\n        }\n\n        throw new IllegalArgumentException(\"Structure \" + getClass()\n                                           + \" has unknown or zero size (ensure \"\n                                           + \"all fields are public)\");\n    }\n\n    /**\n     * Initialize any null-valued fields that should have a non-null default\n     * value.\n     */\n    @SuppressWarnings(\"UseSpecificCatch\")\n    private void initializeFields() {\n        // Get the full field list, don't care about sorting\n        List<Field> flist = getFieldList();\n        for (Field f : flist) {\n            try {\n                Object o = f.get(this);\n                if (o == null) {\n                    initializeField(f, f.getType());\n                }","sourceCodeStart":1453,"sourceCodeEnd":1489,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Structure.java#L1453-L1489","documentation":"After field calculation, the Structure ended up with size zero or unknown — normally because no fields were included in the layout (e.g. all fields non-public, since JNA only lays out public fields) or the structure genuinely has no sizeable fields. IllegalArgumentException names the Structure class.","triggerScenarios":"Structure subclass with zero fields, all fields private/protected (skipped by the fieldInfo loop's Modifier.isPublic check), or all fields deferred as null arrays while forcing size; calling size()/write()/read() on such a structure.","commonSituations":"Making fields private and exposing getters (JavaBean style); forgetting @Structure.FieldOrder/field list entirely on an empty subclass; nested structure with only static or transient members.","solutions":["Declare all native-mapped fields public (JNA skips non-public fields)","Check that the Structure actually has at least one instance field of a supported type","If fields exist but are skipped, remove static/transient modifiers or make them public","Verify field ordering setup (FieldOrder annotation or getFieldOrder override) includes all fields"],"exampleFix":"// before\nclass S extends Structure {\n    private int x; // skipped: not public\n}\n// after\nclass S extends Structure {\n    public int x;\n}","handlingStrategy":"validation","validationCode":"int publicInstanceFields = 0;\nfor (Field f : S.class.getDeclaredFields()) {\n    int m = f.getModifiers();\n    if (Modifier.isPublic(m) && !Modifier.isStatic(m) && !Modifier.isTransient(m)) publicInstanceFields++;\n}\nif (publicInstanceFields == 0) throw new IllegalStateException(\"No public fields in \" + S.class);","typeGuard":"static boolean hasLayableFields(Class<?> c) {\n    for (Field f : c.getFields()) {\n        int m = f.getModifiers();\n        if (Modifier.isPublic(m) && !Modifier.isStatic(m) && !Modifier.isTransient(m)) return true;\n    }\n    return false;\n}","tryCatchPattern":"try {\n    s.size();\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"unknown or zero size\")) {\n        throw new IllegalStateException(\"Add public native-mapped fields to \" + e.getMessage(), e);\n    }\n    throw e;\n}","preventionTips":["Declare structure fields public and non-static","Never create field-less Structure subclasses for mapping","Call size() in unit tests for every Structure definition","Remember JNA only maps public instance fields"],"tags":["jna","structure","zero-size","field-visibility"],"backgroundTag":"schema-validation-failed","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"}