<KG/>
Published on

Null is Not Enough : Best Way to Check Resource Existence In AEM

Authors

Today I came to know an intersting fact about AEM's resourceResolver api. As AEM developers We usually use the resolver.getResource(assetPath) method to get resources, and then do the null checks.

But here's the thing: sometimes, if the asset path is not correct or moved to another location, this method doesn't return null.

lets consider this scenario : You're working on a component that needs an image resource. You might have written code like:

Resource imageResource = resolver.getResource(imagePath);
if (null != imageResource) {
    // Do something with the imageResource
}

The method won't give us null. Instead, it'll give us something called a NonExistingResource, and our check for null won't catch it. This could lead to unexpected issues later on.

How to fix this? It's simple: double-check the resource using existance ResourceUtil api.

Resource imageResource = resolver.getResource(imagePath);
if (null != imageResource && !ResourceUtil.isNonExistingResource(imageResource)) {
    // The resource is good to use!
}

By adding this extra check, we can avoid those tricky bugs caused by invalid resources. This way, our components will work more reliably.

Cheers!

Share

Khalil

Khalil Ganiga

Just another programmer.. This blog expresses my views of various technologies and scenarios I have come across in realtime.

Keep watching this space for more updates.