{"record":{"id":"5b7b6c6e23d41238","repo":"apache/hadoop","slug":"can-not-create-a-path-from-a-null-string","errorCode":null,"errorMessage":"Can not create a Path from a null string","messagePattern":"Can not create a Path from a null string","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/Path.java","lineNumber":169,"sourceCode":"    URI parentUri = parent.uri;\n    String parentPath = parentUri.getPath();\n    if (!(parentPath.equals(\"/\") || parentPath.isEmpty())) {\n      try {\n        parentUri = new URI(parentUri.getScheme(), parentUri.getAuthority(),\n                      parentUri.getPath()+\"/\", null, parentUri.getFragment());\n      } catch (URISyntaxException e) {\n        throw new IllegalArgumentException(e);\n      }\n    }\n    URI resolved = parentUri.resolve(child.uri);\n    initialize(resolved.getScheme(), resolved.getAuthority(),\n               resolved.getPath(), resolved.getFragment());\n  }\n\n  private void checkPathArg( String path ) throws IllegalArgumentException {\n    // disallow construction of a Path from an empty string\n    if ( path == null ) {\n      throw new IllegalArgumentException(\n          \"Can not create a Path from a null string\");\n    }\n    if( path.length() == 0 ) {\n       throw new IllegalArgumentException(\n           \"Can not create a Path from an empty string\");\n    }   \n  }\n  \n  /**\n   * Construct a path from a String.  Path strings are URIs, but with\n   * unescaped elements and some additional normalization.\n   *\n   * @param pathString the path string\n   */\n  public Path(String pathString) throws IllegalArgumentException {\n    checkPathArg( pathString );\n    \n    // We can't use 'new URI(String)' directly, since it assumes things are","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/Path.java#L151-L187","documentation":"org.apache.hadoop.fs.Path refuses to be constructed from a null string. checkPathArg() is the first guard in every Path(String) constructor; a null argument never reaches URI parsing and fails fast with IllegalArgumentException. This is intentional API hardening: a null path almost always means an upstream variable was never set (missing config key, null job property, unset variable).","triggerScenarios":"Calling new Path(null), new Path(someMap.get(\"fs.defaultFS\")) where the key is absent, constructing a Path from a null FileSystem working-directory component, or passing a null filename from a JobConf/Configuration get() that returned null.","commonSituations":"Reading a config property that was never defined (conf.get(\"my.input.path\") returns null by default), a CLI option parsed without a default value, MapReduce driver code where args[0] is missing, or a test that builds Paths from an unpopulated fixture map.","solutions":["Find the null producer: log or breakpoint the expression passed to new Path(...) and check the Configuration key / method that supplied it.","If the value comes from Configuration, set it explicitly (conf.set(\"key\", \"/data/input\") in the job, or <property> in core-site.xml) or use conf.get(\"key\", \"/default/path\") with a default.","Validate external input before use: reject null/empty with a clear error at the CLI/parser boundary instead of letting Path blow up deep in FileSystem code.","If null is legitimately possible and means 'no path', branch on it explicitly rather than constructing a Path."],"exampleFix":"// before\nString in = conf.get(\"mapred.input.dir\");\nFileSystem fs = FileSystem.get(conf);\nPath p = new Path(in); // IllegalArgumentException when property unset\n\n// after\nString in = conf.get(\"mapred.input.dir\");\nif (in == null || in.isEmpty()) {\n  throw new IllegalArgumentException(\n      \"mapred.input.dir is not set; configure it in the job or core-site.xml\");\n}\nPath p = new Path(in);","handlingStrategy":"validation","validationCode":"// before constructing a Path from external/config input\nString raw = conf.get(\"app.input.path\");\nif (raw == null) {\n  throw new IllegalArgumentException(\"app.input.path is not configured\");\n}\nPath p = new Path(raw);","typeGuard":null,"tryCatchPattern":"// only as a last-resort boundary guard\ntry {\n  Path p = new Path(raw);\n} catch (IllegalArgumentException e) {\n  throw new ConfigurationException(\"Invalid path value: null\", e);\n}","preventionTips":["Never pass Configuration.get() results directly to new Path(); validate or use the two-arg get with a default.","Validate CLI arguments (presence and non-emptiness) before building Paths.","Centralize Path construction in one factory method that rejects null/empty with the parameter name in the message."],"tags":["hadoop","hdfs","path","null-argument","illegalargumentexception","configuration"],"backgroundTag":"null-argument-validation","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}