0

all. I'm trying to get a value from a document, that looks like this:

{
      "id" : player:(some number)",
      "name" : "Martinez, Javi",
      "type" : "midfielder",
      "jersey_number" : 8,
      "position" : "Central defender"
    }

I have players in an ArrayList<Document> playersOnPitchHome and want to find one with a particular "jersey_number"

My code (below), however, is giving me a NulPointerException in the row I've highlighted with the //here comment. As far as I understand, the exception is not occuring due to me accessing playersOnPitchHome

The Code:

Document player = playersOnPitchHome.get(0);
       int i;
       for (i=0; i<playersOnPitchHome.size(); i++) {
           player = playersOnPitchHome.get(i);
           int numberTemp = (int) player.get("jersey_number"); //here
           if (numberTemp == playerNumber) break;
           if (i==(playersOnPitchHome.size()-1) && numberTemp != playerNumber) {
              throw new Exception ("there is no such player on the pitch");
          }

    }

Thank you in advance.

Vid Stropnik
  • 182
  • 10
  • 1
    Please provide the full exception stack so we can dig deeper. If the exception is indeed on the `//here` line then `player` is null; otherwise the `get` method is throwing the exception for some reason. – Mark A. Fitzgerald Oct 22 '17 at 01:11
  • `player.get("jersey_number")` returns an Integer, which might or might not be null. If it happens to be null, it cannot be cast to an int, which is why you’re getting that exception. – VGR Oct 22 '17 at 01:16

0 Answers0