在 logcat 中,我得到了null pointer exception
这个代码示例。我正在尝试在其中添加例如oneObject.has(TAG_TITLE)
,Arraylist
但是例如当我打印时currentPost.getid()
,我得到了这个nullpointer excepttion
。有人能帮助我吗。
// 从 URL 获取 JSON 字符串
JSONArray jArray = jParser.getJSONFromUrl(url);
ArrayList<Post> PostList = new ArrayList<Post>();
Post currentPost = new Post();
// looping through All element
for(int i = 0; i < jArray.length(); i++){
try{
JSONObject oneObject = jArray.getJSONObject(i);
// Storing each json item in variable
if(oneObject.has(TAG_ID)){
id = oneObject.getString(TAG_ID);
currentPost.setId(id);
}
else{
id="";
}
if(oneObject.has(TAG_TITLE)){
title = oneObject.getString(TAG_TITLE);
currentPost.setTitle(title);
}
else{
title ="";
}
if(oneObject.has(TAG_CONTENT)){
content = oneObject.getString(TAG_CONTENT);
currentPost.setContent(content);
}
else{
content ="";
}
System.out.println("postlist: "+currentPost.getId());
PostList.add(currentPost);
currentPost = new Post();