在logcat中,我爲此代碼示例獲取null pointer exception
。我試圖在Arraylist
內添加例如oneObject.has(TAG_TITLE)
,但是當我打印currentPost.getid()
時,例如,我得到這個nullpointer excepttion
。有人能幫助我嗎。向ArrayList中添加數據android
//正從URL
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();
嗨imrank,它仍然爲空 – Dimitri
@sophia:把'currentPost.setId(id); System.out.println(「postlist:」+ currentPost.getId());'代碼裏面的其他部分,並檢查你是否獲得價值登錄或不是 –
是的,它是非常感謝你 – Dimitri