Expand my Community achievements bar.

SOLVED

Null value from resource resolver

Avatar

Level 9

I am getting null resourcefrom the resource resolver. Here is the function I am calling, when I debugged I found path and resourseResolver is not null.

when I am at Resource resource = resourceResolver.getResource(path); I am getting resource as null. Attaching screenshot from eclipse

    private Asset getDamAssets(String path, ResourceResolver resourceResolver)
            throws RepositoryException {
        Asset asset = null;
        LOG.info("...getDamAssets at " + path);
        Resource resource = resourceResolver.getResource(path);

        if (resource != null) {
            // Convert resource to asset, returns null if not an asset
            asset = resource.adaptTo(Asset.class);
        }

        return asset;
    }

1 Accepted Solution

Avatar

Correct answer by
Employee

Hi,

Usually that would indicate that either (a) the resource at that path does not exist or (b) it does exist and the user doesn't have read access to it.

Regards,

Justin

View solution in original post

4 Replies

Avatar

Former Community Member

Try resourceResolver.resolve(Path) it will work.

//This takes absolute path, you might be providing absolute path.

Avatar

Level 10

Hi,

path looks fine but I think that resource is not available at the given path and hence its throwing null. Please check if that resource exists at the give path. Also refer here [1]

[1]https://sling.apache.org/apidocs/sling5/org/apache/sling/api/resource/ResourceResolver.html#getResou...)

Avatar

Correct answer by
Employee

Hi,

Usually that would indicate that either (a) the resource at that path does not exist or (b) it does exist and the user doesn't have read access to it.

Regards,

Justin

Avatar

Level 9

resourceResolver.resolve(Path) works but following line now returns null

    asset = resource.adaptTo(Asset.class);

resource is not null, it is going into if loop but it seems that adaptTo Asset returns null now.