int m=valeurs.get(p);
will throw a NullPointerException
if valuers.get(p)
returns null
, due to the attempt to auto un-box the null
value to a primitive int
.
If your method would return an Integer
instead, it would be able to return a null
:
public Integer getValeur(int x, int y) {
Position p=new Position(x,y);
Integer m=valeurs.get(p);
return m;
}
EDIT :
After seeing your comment that you can't change the return type of getValeur
, your only option (other than returning a default value that represents null) is to throw an unchecked exception (or a checked exception if the method you are overriding is already throwing that exception or a super-class of that exception) if valuers.get(p)
returns null
:
public int getValeur(int x, int y) {
Position p=new Position(x,y);
Integer m=valeurs.get(p);
if (m == null) {
throw new IllegalArgumentException ("Invalid position " + x + "," + y);
}
return m;
}