{"record":{"id":"b3dd8ba2f025e774","repo":"java-native-access/jna","slug":"x-display-is-null","errorCode":null,"errorMessage":"X Display is null","messagePattern":"X Display is null","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"contrib/x11/src/jnacontrib/x11/api/X.java","lineNumber":112,"sourceCode":"         */\n        public Display() {\n            x11Display = x11.XOpenDisplay(null);\n\n            if (x11Display == null) {\n                throw new Error(\"Can't open X Display\");\n            }\n        }\n\n        /**\n         * Creates the OOWindowUtils using a given display.\n         *\n         * @param x11Display open display\n         */\n        public Display(X11.Display x11Display) {\n            this.x11Display = x11Display;\n\n            if (x11Display == null) {\n                throw new Error(\"X Display is null\");\n            }\n        }\n\n        /**\n         * Closes the display.\n         */\n        public void close() {\n            x11.XCloseDisplay(x11Display);\n        }\n\n        /**\n         * Flushes the output buffer / event queue.\n         */\n        public void flush() {\n            x11.XFlush(x11Display);\n        }\n\n        /**","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/x11/src/jnacontrib/x11/api/X.java#L94-L130","documentation":"The X.Display(X11.Display) constructor stores the supplied display handle and throws java.lang.Error('X Display is null') if the caller passes null. It guards against using the wrapper without a valid open Xlib display; every method would otherwise crash in native code.","triggerScenarios":"Calling new X.Display(x11Display) with null — typically because a previous XOpenDisplay call returned null (its failure was not checked) and the null handle was passed straight through.","commonSituations":"Passing the result of XOpenDisplay without a null check; chaining constructor calls where an earlier 'Can't open X Display' path produced a null handle.","solutions":["Check the XOpenDisplay return value for null before constructing X.Display, and handle the connection failure there.","Ensure the X environment is valid (DISPLAY set, X server reachable) so the handle is non-null in the first place.","If null may occur, avoid constructing the wrapper and use an optional/fallback code path.","Treat this Error as a programming error: add an assertion/null check at the call site."],"exampleFix":"// before\nX.Display display = new X.Display(x11.XOpenDisplay(null));\n// after\nX11.Display d = x11.XOpenDisplay(null);\nif (d == null) {\n    throw new IllegalStateException(\"Cannot connect to X server (is DISPLAY set?)\");\n}\nX.Display display = new X.Display(d);","handlingStrategy":"type-guard","validationCode":"X11.Display d = x11.XOpenDisplay(name);\nif (d == null) throw new IllegalStateException(\"XOpenDisplay failed for \" + name);","typeGuard":"boolean isValidDisplay(X11.Display d) {\n    return d != null;\n}","tryCatchPattern":"try {\n    X.Display display = new X.Display(maybeNullDisplay);\n} catch (Error e) {\n    if (\"X Display is null\".equals(e.getMessage())) {\n        throw new IllegalStateException(\"Caller passed a null X display handle\", e);\n    }\n    throw e;\n}","preventionTips":["Never pass an unchecked XOpenDisplay result into X.Display.","Null-check handles at every native-to-Java boundary.","Centralize display creation in one factory that validates the handle.","Treat Error throws here as caller bugs — fix at the source."],"tags":["x11","linux","null-check","display"],"backgroundTag":"null-argument","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"}