1

In one of my classes say Test, in static block of it; i am constructing map using an object of type BakidRangeid as key and Integer as value. My hashmap declaration looks as below.

public static HashMap<BakidRangeid, Integer> mapBankIdRangeIdToConfId = new HashMap<BakidRangeid, Integer> ();

And BakidRangeid class looks like

public class BakidRangeid {

private int bankId;
private int rangeId;

public int getBankId() {
    return bankId;
}
public void setBankId(int bankId) {
    this.bankId = bankId;
}
public int getRangeId() {
    return rangeId;
}
public void setRangeId(int rangeId) {
    this.rangeId = rangeId;
}

}

When i am trying to get the value from hashmap mapBankIdRangeIdToConfId by setting instance of an BakidRangeid, i get NullpointerException inspite of the object with values of bankid and rangeid, which i am passing is present in the hashmap. I guess i will have to override equals and hashcode methods of my BakidRangeid class. Anyone kindly confirm. Thanks in advance!

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
Anita
  • 185
  • 1
  • 6
  • 24

2 Answers2

1

Please Use hashCode() and equals() method in BakidRangeid class.

Refer this link

Community
  • 1
  • 1
Kannan Thangadurai
  • 1,117
  • 2
  • 17
  • 36
1

override equals method of class BakidRangeid before using as a key in hashmap.

navintb
  • 129
  • 5