Expand my Community achievements bar.

SOLVED

Use of Optional.Nullable in AEM

Avatar

Level 8

Hello Team,

 

Wanted to check the actual use case of Optional.Nullable in AEM section.

Why Part 1 is preferred over Part 2?  Is it because of developer-friendly code?

I have observed that in Part 1, by mistake if I add jcr:content at the end, i mean: 

String currentPagePath = "/content/abc/us/en/jcr:content"; then application throws error.

 

Part 1:
String currentPagePath = "/content/abc/us/en";
Resource resource = resourceResolver.getResource(currentPagePath);
     
String templateName= Optional.ofNullable(resource.adaptTo(Page.class).getTemplate().getPath()).orElse("");
String pageTitle= Optional.ofNullable(resource.adaptTo(Page.class).getTitle()).orElse("");
 
 
Part 2:
String resourcePath = "/content/abc/us/en";
resource=resourceResolver.getResource(resourcePath+"/jcr:content");
if(resource !=null) {
ValueMap properties = resource.getValueMap();
String pageTitle = properties.get("jcr:title", String.class);
}
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

The use of Optional in Java is a way to deal with null values and avoid NullPointerException. It's a more modern approach and is generally considered more safer to avoid any runtime execution - the flow will continue to next line or function.

 

In general, it's recommended to use Optional when dealing with null values in Java. It makes the code safer and more readable.

Regarding the error when you add jcr:content at the end of the path, this is because jcr:content is a node that contains properties of the page.

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

The use of Optional in Java is a way to deal with null values and avoid NullPointerException. It's a more modern approach and is generally considered more safer to avoid any runtime execution - the flow will continue to next line or function.

 

In general, it's recommended to use Optional when dealing with null values in Java. It makes the code safer and more readable.

Regarding the error when you add jcr:content at the end of the path, this is because jcr:content is a node that contains properties of the page.

Avatar

Community Advisor

Hi, 

This goes beyond AEM and it is more like a general Java question, there is a pretty useful scenario where "Optional" should be adopted, such as returning the type of a method, etc. I would encourage you to read these articles about when it is "better" to use optional over a simple null check. 

 

https://medium.com/javarevisited/4-reasons-why-you-should-use-java-optional-or-not-4e44d51a9645

https://www.baeldung.com/java-optional-uses 



Hope this helps.



Esteban Bustamante

Avatar

Level 10

@Mahesh_Gunaje 
It was introduced in Java 8 and is part of the java.util package.
In Adobe Experience Manager (AEM), Optional.Nullable is a class used for handling nullable values in Java. It allows developers to express the absence of a value explicitly, reducing null pointer exceptions. By leveraging Optional.Nullable, AEM developers can write cleaner and more robust code, handling null values more effectively.

Part1 -> is safe as handling exception allow us to execute subsequent line. it also allows us to handle null pointer exceptions, thereby preventing unknown errors on our site and ensuring that our backend code does not break.
Part2 -> in case of exception sub-sequent code will not get run and site/functionality will get break.