这是一个非常直截了当的问题,但这个错误对我来说是非常神秘的,因为我找不到解决方案或其他任何有此问题的人。我在另一个活动中也使用了一种非常相似的技术,它工作得很好。我正在制作一个向服务器发出POST
请求的android应用程序。响应是JSONObject
,必须解析为一个数字,另一个JSONObject
必须解析,并将其值分配给CurrentGame
个对象的数组。对getJSONObject
的第一次调用工作正常,但在该JSONObject上调用getString
会返回以下错误:
java.lang.NullPointerException: Attempt to write to field 'java.lang.String com.xxxxx.xxxxx.CurrentGame.oppEmail' on a null object reference
这是我的java代码:
private void handleResponse(JSONObject response){
int numGroups = 0;
try{
numGroups = response.getInt("Number");
}catch(JSONException e){
e.printStackTrace();
}
Log.i("Number of Groups", String.valueOf(numGroups));
CurrentGame[] currentGames = new CurrentGame[numGroups];
JSONObject current;
int yourTurn = 0;
for(int i = 0; i < numGroups; i++){
try{
current = response.getJSONObject(String.valueOf(i));
Log.i("Current JSONObject: ", String.valueOf(current));
if(current.has("OppEmail")){
currentGames[i].oppEmail = current.getString("OppEmail");
}
if(current.has("OppName")) {
currentGames[i].oppName = current.getString("OppName");
}
if(current.has("Group")) {
currentGames[i].group = current.getString("Group");
}
if(current.has("YourTurn")) {
yourTurn = current.getInt("YourTurn");
}
if(yourTurn == 0){
currentGames[i].yourTurn = true;
}
else{
currentGames[i].yourTurn = false;
}
}
catch (JSONException e){
e.printStackTrace();
}
}
}
JSONObject.has()
检查不应该至少阻止此错误吗?
我知道第一个getInt()
和getJSONObject
正在运作。继承日志:
06-21 21:58:56.644 20116-20116/com.xxxxx.xxxxx D/Response:﹕ {"Number":2,"0":{"Group":"Test Group 1","OppEmail":"xxxxx@xxxxx.edu","OppName":"MikeyP","YourTurn":0},"1":{"Group":"Test Group 2","OppEmail":"xxxxx@xxxxx.edu","OppName":"MikeyP","YourTurn":1}}
06-21 21:58:56.644 20116-20116/com.xxxxxx.xxxxxt I/Number of Groups﹕ 2
06-21 21:58:56.644 20116-20116/com.xxxxx.xxxxx I/Current JSONObject﹕ {"Group":"Test Group 1","OppEmail":"xxxxxx@xxxxx.edu","OppName":"MikeyP","YourTurn":0}
这是服务器代码:
$games['Number'] = $numgames;
if($numgames > 0){
$i = 0;
while($row = mysqli_fetch_array($getgames)){
$currGame['Group'] = $row['GroupName'];
// Get the opponent's email and username
if($row['Player1'] != $email){
$opponent = $row['Player1'];
$currGame['OppEmail'] = $opponent;
$sql = "SELECT Username FROM users WHERE Email = '".$opponent."'";
$username = mysqli_query($conn, $sql);
$row2 = mysqli_fetch_assoc($username);
$currGame['OppName'] = $row2['Username'];
}
else if($row['Player2'] != $email){
$opponent = $row['Player2'];
$currGame['OppEmail'] = $opponent;
$sql = "SELECT Username FROM users WHERE Email = '".$opponent."'";
$username = mysqli_query($conn, $sql);
$row2 = mysqli_fetch_assoc($username);
$currGame['OppName'] = $row2['Username'];
}
// Determine if it is this player's turn
if($row['CurrentPlayer'] != $email){
$currGame['YourTurn'] = 0;
}
else{
$currGame['YourTurn'] = 1;
}
$games[$i] = $currGame;
$i++;
}
}
//Echo array of groups
header('Content-Type: application/json');
$response = json_encode($games);
echo $response;
提前感谢您对我在这里做错了什么的想法。我知道有关getString()
返回null
的问题有类似的问题,但是阅读完毕后我仍然非常难过。
答案 0 :(得分:1)
问题是由:
引起的currentGames[i].oppEmail = current.getString("OppEmail");
线。
因为currentGames
数组初始化为大小为2但未添加任何类型为CurrentGame
的项目。
而不是使用currentGames[i].oppEmail
创建CurrentGame
类的对象添加所有值,然后将其添加到currentGames
数组中,如:
CurrentGame objCurrentGame=new CurrentGame();
if(current.has("OppEmail")){
objCurrentGame.oppEmail = current.getString("OppEmail");
}
... same for other fields
...
//Add objCurrentGame to Array
currentGames[i]=objCurrentGame;
答案 1 :(得分:0)
以这种方式解析json不健壮且容易出错,建议使用
这样的库因为这些开源库提供了用于此类目的的稳定实现,并且不需要自己重新发明轮子。
示例:
YourPojoClass obj = new Gson().fromJson("{SomeJsonString}", YourPojoClass.class);
通过这种方式,您可以获得强类型的pojo实例。您甚至不需要自己编写POJO类,并且有许多可以从json字符串生成POJO类的在线服务: