Implementing equals method correctly

Avoiding NPE while invoking equals