java - Lazy evaluation not working as it should -
i had evaluate boolean expression frecuently, converted in private method in it's class. here code it's causing me trouble:
//"x", "y" , "team" defined return (map.iswalkable(x,y) && (!map.isoccupied(x,y) || map.getoccupant(x, y).getteam() == team) );
methods should preety self-explanatory purpose of question. now, iswalkable , isoccupied both return boolean, , getoccupant method returning object reference. problem i'm getting nullpointerexception when executing piece of code, , shouldn't happening because isoccupied returns true if , if map.getoccupant != null (that's method returns). language supporting lazy boolean evaluation left right (as assume java is, or @ least that's able read) getoccupant method should never executed whenever return null right?
is more compiler-dependent thought was? should safer if used if statements, or there obvious i'm missing here, maybe operations resolved other way round.
the issue parenthesis. try
return (map.iswalkable(x,y) && (!map.isoccupied(x,y) || map.getoccupant(x, y).getteam() == team));
Comments
Post a Comment