Hi guys, I'm playing around ith optionals a little, but I'm not surewhether this is the correct behavious as I'm getting a null pointer exeption (I thought the point of Optionals was not to get a null pointer.)
SO I'm setting an object to null and before calling toString on it I'm using isPresent() on it. Here is the code:

package optional_test;
import java.util.Optional;

public class Optional_test {

    public static void main(String[] args) {
        Optional<Customer> customer1 = Optional.ofNullable(new Customer("John", "Doe"));
        Optional<Customer> customer2 = null;
        if(customer1.isPresent()){
            System.out.println(customer1.get());
        }
        if(customer2.isPresent()){
            System.out.println(customer2.orElse(new Customer("Unknown", "Unknown")));
        }

    }

}

And the cCustomer class:

package optional_test;

import java.util.Optional;

public class Customer {
    private String name;
    private String surname;
    public Customer(String name, String surname) {
        this.name = name;
        this.surname = surname;
    }
    public Optional<String> getName() {
        return Optional.ofNullable(this.name);
    }
    public void setName(String name) {
        this.name = name;

    }
    public Optional<String> getSurname() {
        return Optional.ofNullable(this.surname);
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }
    @Override
    public String toString(){
        return "Name: " + getName() + ". Surname: " + getSurname();
    }
}

That's not how Optional should be used. The Optional variable itself should never be set to null. Use Optional.empty() to create an Optional that has no value. Ie line 8 should be

Optional<Customer> customer2 = Optional.empty();

PS: The way you are using isPresent() is perfectly legal, but not recommended. It doesn't really gain you much compared to the traditional use of a null value. The recommended use is with a lambda, ie replace lines 9-1 with something like

customer1.ifPresent(System.out::println);

thanks so I made som changes, more or less what yo've suggested, however, what if I want to get some defalt values if the object is null?
Here is what the main class looks like now

package optional_test;
import java.util.Optional;

public class Optional_test {

    public static void main(String[] args) {
        Optional<Customer> customer1 = Optional.ofNullable(new Customer("John", "Doe"));
        Optional<Customer> customer2 = Optional.empty();
            /*if(customer1.isPresent()){
                System.out.println(customer1.orElse(new Customer("Unknown", "Unknown")));
            }*/
        customer1.ifPresent(value -> {
            System.out.println(value.toString());
        });
        /*if(customer2.isPresent()){
            System.out.println(customer2.orElse(new Customer("Unknown", "Unknown")));
        }*/
        customer2.ifPresent(value -> {
            System.out.println(value.toString());
        });     

    }

}

The output is now John doe only which is good but I'd like to get something as a default for when the object is null and it doesn't look like ifPresent allows me

First a quick point...
You never need toString() in a print because print does that anyway. And if you just want to print the value you can use a simple method reference like I showed in the previous post.

customer1.ifPresent(System.out::println);

Anyway... to print the value if present and some other string if it's not, just use orElse eg

System.out.println(customer2.orElse(new Customer("Unknown", "Unknown"))); 

(if you're going to do that more than once you could create a static final constant for the "unknown" customer, eg

static final Customer N_A = new Customer(){
  public String toString(){return "N/A";}
};

then it's just

System.out.println(customer2.orElse(N_A));

Ah OK I understand, thanks for th examples! Th orElse is actually rather useful!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.