handle null in Comparator class I have a class LoanAccount that contains an attributes creationDate and  loanAmount. 
  
  I want to implement a Comparator that sorts the accounts by loan amount in descending order.
  When the loan amounts are the same, sorts the accounts
  ascending by date.


The code look like this:

Class

public class LoanAccount {
    private String id;
    private Date creationDate;
    private BigDecimal loanAmount;
}


Comparator

public class LoanAccountAmountComparator implements Comparator&lt;LoanAccount&gt; {

    @Override
    public int compare(LoanAccount o1, LoanAccount o2) {
        if (o1 == null) {
            if (o2 == null) {
                return 0;
            }
            return -1;
        }
        if (o2 == null) {
            return 1;
        }
        if (o1.getLoanAmount() == null) {
            if (o2.getLoanAmount() == null) {
                return 0;
            }
            return -1;
        }
        if (o2.getLoanAmount() == null) {
            return 1;
        }

        int comparedLoanAmount = o2.getLoanAmount().compareTo(o1.getLoanAmount());
        if (comparedLoanAmount == 0) {
            if (o1.getCreationDate() == null) {
                if (o2.getCreationDate() == null) {
                    return 0;
                }
                return -1;
            }
            if (o2.getCreationDate() == null) {
                return 1;
            }
            return o1.getCreationDate().compareTo(o2.getCreationDate());
        } else {
            return comparedLoanAmount;
        }
    }
}


Objects o1, o2 can be null. Fields creationDate and loanAmount can be null.

I want to ask you if there are better ways to handle null pointer exception in compareTo method.

For me it looks like there are too many if conditions
it is possible to handle null pointer exception using Comparator.comparing static method. If you want to comparing loanAmount fields in your objects and you want that every not null value is greater than null you can use Comparator.nullFirst method in combination with Comparator.comparing like the below code:

Comparator.comparing(LoanAccount::getLoanAmount,
                     Comparator.nullsFirst(Comparator.naturalOrder()))


NaturalOrder is a method returning a comparator that compares Comparable objects in natural order.
You can also combine more comparators in a chain with method Comparator.thenComparing like the code below:

Comparator.comparing(LoanAccount::getLoanAmount,
                Comparator.nullsFirst(Comparator.naturalOrder()))
                .thenComparing(LoanAccount::getCreationDate, 
                Comparator.nullsFirst(Comparator.naturalOrder()))
                .compare(o1, o2);


Now you can rewrite your comparator with equal behaviour shortly:

public class LoanAccountAmountComparator implements Comparator&lt;LoanAccount&gt; {
    @Override
    public int compare(LoanAccount o1, LoanAccount o2) {
        if (o1 == null &amp;&amp; o2 == null) return 0;
        if (o1 == null) return -1;
        if (o2 == null) return 1;
        return Comparator.comparing(LoanAccount::getLoanAmount,
                Comparator.nullsFirst(Comparator.naturalOrder()))
                .thenComparing(LoanAccount::getCreationDate, 
                Comparator.nullsFirst(Comparator.naturalOrder()))
                .compare(o1, o2);
    }
}


Note: the use of class Date is discouraged, it is better if you use instead the java.time package classes for time related code.