{"record":{"id":"ee489c507573f87a","repo":"elastic/elasticsearch","slug":"cannot-cast-null-java-lang-string-to-char","errorCode":null,"errorMessage":"cannot cast null java.lang.String to char","messagePattern":"cannot cast null java\\.lang\\.String to char","errorType":"exception","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"modules/lang-painless/src/main/java/org/elasticsearch/painless/Utility.java","lineNumber":24,"sourceCode":" * your election, the \"Elastic License 2.0\", the \"GNU Affero General Public\n * License v3.0 only\", or the \"Server Side Public License, v 1\".\n */\n\npackage org.elasticsearch.painless;\n\n/**\n * A set of methods for non-native boxing and non-native\n * exact math operations used at both compile-time and runtime.\n */\npublic class Utility {\n\n    public static String charToString(final char value) {\n        return String.valueOf(value);\n    }\n\n    public static char StringTochar(final String value) {\n        if (value == null) {\n            throw new ClassCastException(\n                \"cannot cast \" + \"null \" + String.class.getCanonicalName() + \" to \" + char.class.getCanonicalName()\n            );\n        }\n\n        if (value.length() != 1) {\n            throw new ClassCastException(\n                \"cannot cast \" + String.class.getCanonicalName() + \" with length not equal to one to \" + char.class.getCanonicalName()\n            );\n        }\n\n        return value.charAt(0);\n    }\n\n    private Utility() {}\n}\n","sourceCodeStart":6,"sourceCodeEnd":40,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/modules/lang-painless/src/main/java/org/elasticsearch/painless/Utility.java#L6-L40","documentation":"Utility.StringTochar is the runtime helper Painless emits when a script casts a String value to char. If the String is null, this ClassCastException is thrown with the literal type names (java.lang.String and char). This fires at script execution time, not compile time.","triggerScenarios":"A Painless script performs an explicit cast '(char)someString' or assigns a String to a char variable where the String value is null at runtime — e.g. reading a missing document field that returns null.","commonSituations":"Casting doc['field'].value (which can be null for missing fields) to char; user-supplied script params that are null; runtime field evaluating null string data.","solutions":["Null-check the String before casting to char.","Default the source value with an elvis/conditional: str == null ? '\\0' : (char)str.","Ensure the underlying field/mapping cannot produce null for the scripted path."],"exampleFix":"// before\nchar c = (char)doc['initial'].value; // null -> 1348\n// after\nString s = doc['initial'].value;\nchar c = s == null ? '\\\\0' : (char)s;","handlingStrategy":"validation","validationCode":"// Inside the Painless script, guard before casting:\n// String s = doc['initial'].value;\n// if (s == null) { /* handle */ } else { char c = (char)s; }","typeGuard":"// Painless-side guard\nString s = doc['initial'].value;\nif (s != null && s.length() == 1) { /* safe to cast */ }","tryCatchPattern":"// In Java calling code, catch ClassCastException from script execution:\ntry {\n    scriptService.run(script, context, vars);\n} catch (ScriptException e) {\n    if (e.getCause() instanceof ClassCastException ce && ce.getMessage().contains(\"null\") && ce.getMessage().contains(\"char\")) {\n        log.warn(\"null String-to-char in script\");\n    }\n    throw e;\n}","preventionTips":["Never cast a possibly-null String to char without a null check.","Default missing fields explicitly before scripted casts.","Map fields used as char sources as non-null in your data model."],"tags":["painless","scripting","runtime","cast","null","elasticsearch"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}