我有以下代碼:GSON isJsonObject()拋出的NullPointerException
final JsonObject source = source.toJson();
final JsonElement relatedSpace = source.get("myField");
if(relatedSpace.isJsonObject()){
//do something
}
isJsonObject()
確實return instanceof JsonObject;
根據所述GSON代碼。 instanceof
應該先做一個null
檢查,所以我不確定在relatedSpace
是null
時我的代碼是否可能拋出NullPointerException
。我可以輕鬆地檢查null
首先解決問題,但我想知道爲什麼會發生這種情況。