I am creating an API that receives the following input, and provides the following output.
So I need to have a function for making guesses. I have not had any luck testing my code so far.
When I send a POST (using POSTMan) as such to http://localhost:8080/guess with the body as {"game":"klubxb", "guess":"a"}
. I have Content-type
set to application/json
and the body is raw
with {"game":"lmzxmn","guess":"c"}
This is the response:
{
"timestamp": "2018-04-28T00:40:29.141+0000",
"status": 500,
"error": "Internal Server Error",
"message": "No message available",
"path": "/guess"
}
The function I have defined for making guesses is:
@RequestMapping(value = "/guess", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
public Game makeGuess(@RequestBody Guess gameAndLetter, HttpSession session) throws GameDoesNotExistException, InvalidCharacterException{
String game = gameAndLetter.getGame();
String guess = gameAndLetter.getGuess();
Game g = getGame(game,session);
String gameId = g.getId();
if(gameId.equals(game) && guess.length() > 0) {
boolean correct = compareWords(guess, g);
if(!correct){
g.incIncorrect_guesses();
}
g.setStatus();
}
else{
if(!gameId.equals(game)) {
throw new GameDoesNotExistException(game);
}
else{
throw new InvalidCharacterException(guess);
}
}
g = getGame(game,session);
return g;
}
I receive a NullPointerException from the serverside:
https://pastebin.com/sczmbDri
This is the code for getGame:
// Find an existing game
private Game getGame(String id, HttpSession session) throws GameDoesNotExistException{
List<Game> games = (List<Game>) session.getAttribute("games");
Game g = null;
for(int i = 0; i < games.size(); i++){
g = games.get(i);
if(g.getId().equals(id)){
break;
}
}
if (g == null) {
throw new GameDoesNotExistException(id);
}
return g;
}