Your method doesn't return anything except in one possible scenario. It has to return something (or throw an exception) in all possible scenarios.
I think you meant to do this:
public Card playCard(int id){
for (Card element : hand) {
if (element.getID() == id) {
return element;
}
}
throw new SomeAppropriateException("Card does not exist in hand");
}
...but I'm guessing a bit (as I don't know what hand
is, but it looked a lot like a List
). That code will always either execute the return
statement or throw an exception, there's no way to get to the end of the method without one of those things happening.
Note that throwing a NullPointerException
for a condition that isn't caused by a null
pointer is a Bad Idea(tm). (It's also best to be consistent in where you put your {
and }
.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…