Make Selenium Easy

Handle Horror Of NullPointerException Using Optional Class

Introduction

Everyone knows the horror of NullPointerException. If we do not put checks on the null value then the execution of the program will be terminated abruptly and sometimes we require a lot of debugging to identify which variable is going null.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

Program without null check

See the below code. I am not putting any null check on the global static variable and trying to get the length of a string “firstName”.

Output

We can have a null check on the above variable to avoid NullPointerException.

In the above program, we will not encounter NullPointerException as we have a null check now.

In real-time we may have many variables which might be null and putting checks for all is not an easy task. We have another option to handle these effectively using the Optional class which was introduced in Java 8.

Optional Class

Optional is a final class that was introduced in Java 8. It is a container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value. Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (return a default value if value not present) and ifPresent() (execute a block of code if the value is present).

The last lines of the above paragraph will make more sense with examples that I will cover now.

There is a static method in the Optional class named ofNullable() that returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional. In simple words, if the value is not null then return the value otherwise return empty.

Now you will get output as “FirstName has no value.“.

If we see the implementation of isNullable() method then you can find it check for null internally.

In fact, I don’t find it useful as checking for null explicitly or using Optional class are the same and I feel there are extra lines of code involved in Optional class. But there are some methods which I really like and want to share here.

orElse(T other) Method

I really like this method of Optional class and use it extensively in coding. This method can be used to return other values in case of null. It is really helpful in providing default values to variables.

We will get output as “Amod” as firstName is null.

There are many methods in Optional class and we will see some useful methods in upcoming posts.

Usage in Selenium

We can use this in Selenium scripts to handle data dependency. Suppose for registering a user we may require some details like name, address, etc. This data needs not be passed through data sources always. Our script should take data if provided or use some default or random data in case no data is passed. We can integrate Faker API of Java with it to generate proper random data for our tests.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Handle Horror Of NullPointerException Using Optional Class

by Amod Mahajan time to read: 3 min
0
%d bloggers like this: