java.lang.NullPointerException:尝试调用虚拟方法';int org.json.JSONArray.length();关于空对象引用
每次我打开我的应用程序它崩溃,因为这个错误,我不知道我该怎么做,请帮助我。。。我还附上了下面的logcat屏幕截图和发生错误的java类 DataParser.java:java.lang.NullPointerException:尝试调用虚拟方法';int org.json.JSONArray.length();关于空对象引用,json,nullpointerexception,long-integer,Json,Nullpointerexception,Long Integer,每次我打开我的应用程序它崩溃,因为这个错误,我不知道我该怎么做,请帮助我。。。我还附上了下面的logcat屏幕截图和发生错误的java类 DataParser.java: public class DataParser { private HashMap<String,String> getPlace(JSONObject googlePlacesJson){ HashMap<String,String> googlePlaceMap=new H
public class DataParser {
private HashMap<String,String> getPlace(JSONObject googlePlacesJson){
HashMap<String,String> googlePlaceMap=new HashMap<>();
String placeName="-NA-";
String vicinity="-NA-";
String latitude="";
String logitude="";
String reference="";
List<HashMap<String,String>> placesList=new ArrayList<>();
try {
if(!googlePlacesJson.isNull("name")){
placeName=googlePlacesJson.getString("name");
}
if (!googlePlacesJson.isNull("vicinity")){
vicinity=googlePlacesJson.getString("vicinity");
}
latitude=googlePlacesJson.getJSONObject("geometry").getJSONObject("location").getString("lat");
logitude=googlePlacesJson.getJSONObject("geometry").getJSONObject("location").getString("lng");
reference=googlePlacesJson.getString("reference");
googlePlaceMap.put("name",placeName);
googlePlaceMap.put("vicinity",vicinity);
googlePlaceMap.put("lat",latitude);
googlePlaceMap.put("lng",logitude);
googlePlaceMap.put("reference",reference);
} catch (JSONException e) {
e.printStackTrace();
}
return googlePlaceMap;
}
private List<HashMap<String,String>> getPlaces(JSONArray jsonArray){
long count;
count=jsonArray.length(); // error comes on this line
List<HashMap<String,String>> placesList=new ArrayList<>();
HashMap<String,String> placeMap=null;
for(int i=0;i<count;i++){
try {
placeMap=getPlace((JSONObject) jsonArray.get(i));
placesList.add(placeMap);
} catch (JSONException e) {
e.printStackTrace();
}
}
return placesList;
}
public List<HashMap<String,String>> parse(String jsonData){
JSONArray jsonArray=null;
JSONObject jsonObject;
try {
jsonObject=new JSONObject(jsonData);
jsonArray=jsonObject.getJSONArray("results");
} catch (JSONException e) {
e.printStackTrace();
}
return getPlaces(jsonArray);
}
}
公共类数据解析器{
私有HashMap getPlace(JSONObject googlePlacesJson){
HashMap googlePlaceMap=新HashMap();
字符串placeName=“-NA-”;
字符串附近=“-NA-”;
字符串纬度=”;
字符串logitude=“”;
字符串引用=”;
List placesList=new ArrayList();
试一试{
如果(!googlePlacesJson.isNull(“名称”)){
placeName=googlePlacesJson.getString(“名称”);
}
如果(!googlePlacesJson.isNull(“邻近”)){
邻近性=googlePlacesJson.getString(“邻近性”);
}
纬度=googlePlacesJson.getJSONObject(“几何”).getJSONObject(“位置”).getString(“纬度”);
logitude=googlePlacesJson.getJSONObject(“几何”).getJSONObject(“位置”).getString(“lng”);
reference=googlePlacesJson.getString(“reference”);
googlePlaceMap.put(“名称”,地名);
googlePlaceMap.put(“邻近”,邻近);
Google PlaceMap.put(“纬度”,纬度);
googlePlaceMap.put(“lng”,logitude);
googlePlaceMap.put(“参考”,参考);
}捕获(JSONException e){
e、 printStackTrace();
}
返回谷歌地图;
}
私有列表getPlaces(JSONArray JSONArray){
长计数;
count=jsonArray.length();//此行出现错误
List placesList=new ArrayList();
HashMap placeMap=null;
对于(int i=0;i而言,问题就在这里的某个地方:
public List<HashMap<String,String>> parse(String jsonData){
JSONArray jsonArray=null;
JSONObject jsonObject;
try {
jsonObject=new JSONObject(jsonData);
jsonArray=jsonObject.getJSONArray("results"); // !!! this line here !!!
} catch (JSONException e) {
e.printStackTrace();
}
return getPlaces(jsonArray);
}
公共列表解析(字符串jsonData){
JSONArray JSONArray=null;
JSONObject JSONObject;
试一试{
jsonObject=新的jsonObject(jsonData);
jsonArray=jsonObject.getJSONArray(“结果”);/!!!这一行在这里!!!
}捕获(JSONException e){
e、 printStackTrace();
}
返回getPlaces(jsonArray);
}
getJSONArray(“结果”);
返回null
,这会导致以后出现问题(NullPointerException)
你应该做什么:
检查JSON的语法错误。
在getJSONArray()行上放置一个断点,然后查看jsonData
和jsonObject
,如果一切都是,那么它应该是。
您要检索的数组可能无效或根本不在jsonData中。在运行整个方法之前,我首先验证jsonArray
是否为空。或者:
if(jsonArray!=null)
count=jsonArray.length();
else计数=0;
然后进行调试,找出为什么使用空对象调用该方法