Skip to content

Spring 6.2.10 getResource(String location): returns null #35467

@StoynovAngel

Description

@StoynovAngel

Environment:

  • Spring Framework: 6.2.10
  • Java: 17
package org.springframework.core.io;

	/**
	 * Return a {@code Resource} handle for the specified resource location.
	 * <p>The handle should always be a reusable resource descriptor,
	 * allowing for multiple {@link Resource#getInputStream()} calls.
	 * <p><ul>
	 * <li>Must support fully qualified URLs, for example, "file:C:/test.dat".
	 * <li>Must support classpath pseudo-URLs, for example, "classpath:test.dat".
	 * <li>Should support relative file paths, for example, "WEB-INF/test.dat".
	 * (This will be implementation-specific, typically provided by an
	 * ApplicationContext implementation.)
	 * </ul>
	 * <p>Note that a {@code Resource} handle does not imply an existing resource;
	 * you need to invoke {@link Resource#exists} to check for existence.
	 * @param location the resource location
	 * @return a corresponding {@code Resource} handle (never {@code null})
	 * @see #CLASSPATH_URL_PREFIX
	 * @see Resource#exists()
	 * @see Resource#getInputStream()
	 */
	Resource getResource(String location);
location = applicationContext.getEnvironment().resolvePlaceholders(location);
Resource resource = applicationContext.getResource(location);

if (resource != null && resource.exists()) {
	resources.add(resource);
}

Even though getResource could not return null, the exception occur.

java.lang.NullPointerException: Cannot invoke "org.springframework.core.io.Resource.exists()" because "resource" is null

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions