Use of Optional.Nullable in AEM | Community
Skip to main content
Level 7
February 29, 2024
Solved

Use of Optional.Nullable in AEM

  • February 29, 2024
  • 3 replies
  • 1167 views

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);
}
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SureshDhulipudi

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.

3 replies

SureshDhulipudi
Community Advisor
SureshDhulipudiCommunity AdvisorAccepted solution
Community Advisor
February 29, 2024

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.

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
February 29, 2024

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
Imran__Khan
Community Advisor
Community Advisor
February 29, 2024

@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.