从Document获取值时出现Null Pointer Exception

时间:2017-10-22 00:57:56

标签: java

所有。 我试图从文档中获取一个值,如下所示:

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

我有ArrayList<Document> playersOnPitchHome的玩家,想要找到具有特定"jersey_number"的玩家

然而,我的代码(下面)在我用//here评论突出显示的行中给了我一个NulPointerException。据我了解,由于我访问playersOnPitchHome

,因此未发生异常

守则:

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");
          }

    }

提前谢谢。

0 个答案:

没有答案