Avoid NPE in Address.hashCode()

Fixes #1874
This commit is contained in:
cketti 2016-12-28 22:21:12 +01:00
parent b2ed230b32
commit 40e5f03804
2 changed files with 21 additions and 1 deletions

View file

@ -173,7 +173,10 @@ public class Address implements Serializable {
@Override
public int hashCode() {
int hash = mAddress.hashCode();
int hash = 0;
if (mAddress != null) {
hash += mAddress.hashCode();
}
if (mPersonal != null) {
hash += 3 * mPersonal.hashCode();
}

View file

@ -7,6 +7,7 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@RunWith(RobolectricTestRunner.class)
@ -89,4 +90,20 @@ public class AddressTest {
assertEquals("\"sa\"mp\"le\"", Address.quoteString("\"sa\"mp\"le\""));
assertEquals("\"\"\"", Address.quoteString("\""));
}
@Test
public void hashCode_withoutAddress() throws Exception {
Address address = Address.parse("name only")[0];
assertNull(address.getAddress());
address.hashCode();
}
@Test
public void hashCode_withoutPersonal() throws Exception {
Address address = Address.parse("alice@example.org")[0];
assertNull(address.getPersonal());
address.hashCode();
}
}