{"record":{"id":"12d39095cb50b4a5","repo":"apache/hadoop","slug":"can-not-read-resource-file-because-given-clas","errorCode":null,"errorMessage":"Can not read resource file '{}' because given class loader is null","messagePattern":"Can not read resource file '(.+?)' because given class loader is null","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ThreadUtil.java","lineNumber":116,"sourceCode":"    return getResourceAsStream(cl, resourceName);\n  }\n\n  /**\n   * Convenience method that returns a resource as inputstream from the\n   * classpath using given classloader.\n   * <p>\n   *\n   * @param cl ClassLoader to be used to retrieve resource.\n   * @param resourceName resource to retrieve.\n   *\n   * @throws IOException thrown if resource cannot be loaded\n   * @return inputstream with the resource.\n   */\n  public static InputStream getResourceAsStream(ClassLoader cl,\n        String resourceName)\n        throws IOException {\n    if (cl == null) {\n      throw new IOException(\"Can not read resource file '\" + resourceName +\n          \"' because given class loader is null\");\n    }\n    InputStream is = cl.getResourceAsStream(resourceName);\n    if (is == null) {\n      throw new IOException(\"Can not read resource file '\" +\n          resourceName + \"'\");\n    }\n    return is;\n  }\n\n\n}\n","sourceCodeStart":98,"sourceCodeEnd":129,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/ThreadUtil.java#L98-L129","documentation":"The two-arg ThreadUtil.getResourceAsStream(ClassLoader, String) requires a non-null classloader; passing null throws this IOException immediately. Code reaches this with an explicitly passed null, a never-initialized loader field, or via the one-arg overload when the thread context classloader is null (which forwards the same null here).","triggerScenarios":"ThreadUtil.getResourceAsStream(null, \"a.xml\"); calling with a ClassLoader field that was never assigned; a loader obtained from a detached context that legitimately returns null; chained from error 1845's one-arg variant.","commonSituations":"Optional-dependency code that resolves a loader reflectively and gets null; passing Thread.currentThread().getContextClassLoader() without a null check; refactor that dropped the loader initialization but kept the call.","solutions":["Pass a real loader: getClass().getClassLoader(), or the TCCL when known non-null.","Null-check the loader before the call and pick a fallback loader (system classloader or the class's own).","Fix the one-arg call path (error 1845) if this surfaces indirectly.","Catch IOException around resource loading to degrade features that need the resource."],"exampleFix":"// before\nClassLoader cl = resolveLoader(); // returns null\nInputStream is = ThreadUtil.getResourceAsStream(cl, \"a.xml\");\n// throws: given class loader is null\n\n// after\nClassLoader cl = resolveLoader();\nif (cl == null) cl = MyApp.class.getClassLoader();\nInputStream is = ThreadUtil.getResourceAsStream(cl, \"a.xml\");","handlingStrategy":"validation","validationCode":"if (cl == null) {\n  cl = MyApp.class.getClassLoader();\n}\nInputStream is = ThreadUtil.getResourceAsStream(cl, resourceName);","typeGuard":null,"tryCatchPattern":"try {\n  is = ThreadUtil.getResourceAsStream(cl, resourceName);\n} catch (IOException e) {\n  LOG.error(\"Cannot load resource \" + resourceName + \" via \" + cl, e);\n  throw e;\n}","preventionTips":["Objects.requireNonNull(loader) at the call boundary so nulls fail with context.","Resolve the classloader once at startup and share the non-null reference.","When loader discovery may legitimately fail, code an explicit fallback loader."],"tags":["classloader","classpath","null-check","resources","hadoop-common"],"backgroundTag":"null-classloader","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}