DEV Community

arpitmandliya
arpitmandliya

Posted on

Java 8 Optional: A way to avoid NullPointerException

In this post, we will see about Java 8 optional.

Did you ever get NullPointerException as Java developer? If you are experienced Java developer, you might have got NullPointerException at some point of time.
You might agree that NullPointerException is pain for novice or expert core java developer. You have to put a lot of defensive code in case you want to avoid NullPointerException.
Let's see with the help of an example.

package org.arpit.java2blog;

public class Employee {
    private String name;
    private int age;
    
    public Employee(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

Create a main class named "JavaFindEmployeeMain.java"

package org.arpit.java2blog;
 
import java.util.ArrayList;
import java.util.List;
 
public class JavaFindEmployeeMain {
    
    public static void main(String[] args)
    {
        List<Employee> employeeList = createEmployeeList();
        Employee employee = findEmployee(employeeList,"Adam");
        System.out.println("Employee name: "+employee.getName());
    }
    
    public static Employee findEmployee(List<Employee> employeeList,String name)
    {
        for(Employee e:employeeList)
        {
            if(e.getName().equalsIgnoreCase(name))
            {
                return e;
            }
        }
        return null;
    }
    public static List<Employee> createEmployeeList()
    {
        List<Employee> employeeList=new ArrayList<>();
        
        Employee e1=new Employee("John",21);
        Employee e2=new Employee("Martin",22);
        Employee e3=new Employee("Mary",31);
        Employee e4=new Employee("Stephan",18);
        Employee e5=new Employee("Gary",26);
        
        employeeList.add(e1);
        employeeList.add(e2);
        employeeList.add(e3);
        employeeList.add(e4);
        employeeList.add(e5);
        
        return employeeList;
        
    }
}

When you run above program, you will get below output:

Exception in thread "main" java.lang.NullPointerException
at org.arpit.java2blog.JavaOptionalMain.main(JavaOptionalMain.java:12)

As you can see, "Adam" is not present in employeeList, that's why we are getting NullPointerException here.

Do you see the issue here? We forgot to check null when we tried to find employee in the list. This occurs more often when you call library function and it returns null and you forget to check it.


Java 8 Optional

You can use Optional to solve this problem. You can change "JavaOptionalMain" as below.

package org.arpit.java2blog;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
 
public class JavaOptionalMain {
    
    public static void main(String[] args)
    {
        List<Employee> employeeList = createEmployeeList();
        Optional<Employee> employeeOpt = findEmployee(employeeList,"Adam");
        if(employeeOpt.isPresent())
        {
            Employee employee = employeeOpt.get();
            System.out.println("Employee name: "+employee.getName());
        }
        else
        {
            System.out.println("There is no employee with name Adam");
        }
    }
    
    public static Optional<Employee> findEmployee(List<Employee> employeeList,String name)
    {
        for(Employee e:employeeList)
        {
            if(e.getName().equalsIgnoreCase(name))
            {
                return Optional.of(e);
            }
        }
        return Optional.empty();
    }
    public static List<Employee> createEmployeeList()
    {
        List<Employee> employeeList=new ArrayList<>();
        
        Employee e1=new Employee("John",21);
        Employee e2=new Employee("Martin",22);
        Employee e3=new Employee("Mary",31);
        Employee e4=new Employee("Stephan",18);
        Employee e5=new Employee("Gary",26);
        
        employeeList.add(e1);
        employeeList.add(e2);
        employeeList.add(e3);
        employeeList.add(e4);
        employeeList.add(e5);
        
        return employeeList;
    }
}

When you run above program, you will get below output:

There is no employee with name Adam

You might think that you could have handled null in JavaFindEmployeeMain as well but when you return Optional from method, it means that missing value can be expected from method.
Optional Java 8


Ways to create Optional

There are multiple ways to create Optional.

Empty Optional

You can create empty optional object using static factory method "empty"

Optional<Employee> optCar = Optional.empty();

Optional from a non-null value

You can create Optional from non-null value using static factory method "of"

Optional<Employee> optCar = Optional.of(employee);

If employee is null then above method will throw NullPointerException.

Optional from null or non-null value

You can create Optional from null or non null value using static factory method "ofNullable"

Optional<Employee> optCar = Optional.ofNullable(employee);

Getting value from Optional

You can use get() method to retrieve value from Optional but it is least safe. If value is not present then it will throw NoSuchElementException, so you need to make sure you call isPresent() method before you call get() method.


Check value in Optional

You can check if there is value wrapped inside Optional using isPresent method.

    public static void main(String[] args)
    {
            List<Employee> employeeList = createEmployeeList();
        Optional<Employee> employeeOpt = findEmployee(employeeList,"Adam");
        if(employeeOpt.isPresent())
        {
            Employee employee = employeeOpt.get();
            System.out.println("Employee name: "+employee.getName());
        }
        else
        {
            System.out.println("There is no employee with name Adam");
        }
    }

Conditional action in Optional

You can use ifPresent method to execute action if value is present in Optional.
Change main method in JavaOptionalMain as below

      
    public static void main(String[] args)
    {
        List<Employee> employeeList = createEmployeeList();
        Optional<Employee> employeeOpt1 = findEmployee(employeeList,"Adam");
        Optional<Employee> employeeOpt2 = findEmployee(employeeList,"John");
        
        employeeOpt1.ifPresent((employee)->System.out.println("Employee name: "+employee.getName()+" found in list"));
        employeeOpt2.ifPresent((employee)->System.out.println("Employee name: "+employee.getName()+" found in list"));

    }

When you run this program, you will get below output:

Employee name: Dummy
Employee name: John found in list

As you can see here, if employee name present in the list, then only we are printing employee name else it does not perform any action.


Default value in Optional using orElse

You can return default value in case there is no value in Optional using orElse method.
Change main method in JavaOptionalMain as below

     
    public static void main(String[] args)
    {
        List<Employee> employeeList = createEmployeeList();
        Optional<Employee> employeeOpt = findEmployee(employeeList,"Adam");
        Employee employee1 = employeeOpt.orElse(new Employee("Dummy",0));
        System.out.println("Employee name: "+employee1.getName());
        
        Optional<Employee> employeeOpt2 = findEmployee(employeeList,"Martin");
        Employee employee2= employeeOpt2.orElse(new Employee("Dummy",0));
        System.out.println("Employee name: "+employee2.getName());
        
    }

When you run this program, you will get below output:

Employee name: Dummy
Employee name: Martin

Please note that even if value is present in Optional, default object will be crerted.


Default value in Optional using orElseGet

orElseGet is lazy counter part of orElse.It takes supplier as parameter and will be called only if value is not present in Optional.
Change main method in JavaOptionalMain as below

    
    public static void main(String[] args)
    {
        List<Employee> employeeList = createEmployeeList();
        Optional<Employee> employeeOpt = findEmployee(employeeList,"Adam");
        Employee employee1 = employeeOpt.orElseGet(()->new Employee("Dummy",0));
        System.out.println("Employee name: "+employee1.getName());
        
        Optional<Employee> employeeOpt2 = findEmployee(employeeList,"Martin");
        Employee employee2 = employeeOpt2.orElseGet(()->new Employee("Dummy",0));
        System.out.println("Employee name: "+employee2.getName());
        
    }

When you run this program, you will get below output:

Employee name: Dummy
Employee name: Martin


Throwing exception from Optional

You can use orElseThrow to throw exception in case Optional is empty. It is similar to get() method but in this case, you can choose to throw any Exception rathar than NoSuchMethodException.
Change main method in JavaOptionalMain as below

    public static void main(String[] args)
    {
        List<Employee> employeeList = createEmployeeList();
        Optional<Employee> employeeOpt = findEmployee(employeeList,"Adam");
        Employee employee1 = employeeOpt.orElseThrow(() -> new RuntimeException("Employee not found"));
        System.out.println("Employee name: "+employee1.getName());
        
        Optional<Employee> employeeOpt2 = findEmployee(employeeList,"Martin");
        Employee employee2 = employeeOpt2.orElseThrow(() -> new RuntimeException("Employee not found"));
        System.out.println("Employee name: "+employee2.getName());
        
    }

Use of Optional in Java 8 APIs

There are lot of uses of Optional in Java 8 APIs.Let me provide you one example.
Stream.findFirst() method returns an Optional with the first element of this stream, or an empty Optional if the stream is empty.

That's all about Optional in Java 8.

Happy learning!!

Oldest comments (2)

Collapse
 
cubiclebuddha profile image
Cubicle Buddha

Yes, optional is a great pattern. Have you tried Kotlin, Swift, or TypeScript which have the choice of non-nullable references? It’s a much more effective way of eliminating null references. That being said, I still use Optional or Either pattern in TypeScript when I want to return one of two types. I’m glad to find another fan of this defensive programming technique. :)

Collapse
 
manhdat107 profile image
Vu Manh Dat • Edited

great article, but I have a little confused about this. when you check optional is present, in this case, I can check this is null or not ... why don't do that? cuz looks quite the same.