请帮助我为什么会收到此错误。我正在使用convertStreamToString
获取正确字符串的方法将 Json 响应转换为字符串,我可以在我的 logcat 中看到这个字符串。
现在我正在尝试将我的 json 响应字符串插入到 sqlite 数据库中,因为我正在使用
JSONObject HabitatObject = MyHabitatJsonObject.getJSONObject("HabitatDetails");
JSONObject HabitatPosts = HabitatObject.getJSONObject("habitat");
JSONArray HabitatPostsArray = HabitatPosts.getJSONArray("Post");
ContentValues values = new ContentValues();
DatabaseHelper InsertHabitat;
for (int i = 0; i < HabitatPostsArray.length(); i++) {
values.put(InsertHabitat.PostUserImage, HabitatPostsArray.getJSONObject(i).getString("userimage").toString());
values.put(InsertHabitat.PostUserName, HabitatPostsArray.getJSONObject(i).getString("username").toString());
dbHabitat.insert(InsertHabitat.HABITATTABLE, null, values);
}
到目前为止,一切都很好,但是当我尝试将这些值插入数据库时,我在这一行出现错误values.put(InsertHabitat.PostUserImage, HabitatPostsArray.getJSONObject(i).getString("userpostimage").toString());
错误日志:
12-10 10:57:19.872: E/AndroidRuntime(301): FATAL EXCEPTION: AsyncTask #3
12-10 10:57:19.872: E/AndroidRuntime(301): java.lang.RuntimeException: An error occured while executing doInBackground()
12-10 10:57:19.872: E/AndroidRuntime(301): at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-10 10:57:19.872: E/AndroidRuntime(301): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-10 10:57:19.872: E/AndroidRuntime(301): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-10 10:57:19.872: E/AndroidRuntime(301): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-10 10:57:19.872: E/AndroidRuntime(301): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-10 10:57:19.872: E/AndroidRuntime(301): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-10 10:57:19.872: E/AndroidRuntime(301): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-10 10:57:19.872: E/AndroidRuntime(301): at java.lang.Thread.run(Thread.java:1096)
12-10 10:57:19.872: E/AndroidRuntime(301): Caused by: java.lang.NullPointerException
12-10 10:57:19.872: E/AndroidRuntime(301): at com.UserLogin.Habitat$HabitatResults.doInBackground(Habitat.java:90)
12-10 10:57:19.872: E/AndroidRuntime(301): at com.UserLogin.Habitat$HabitatResults.doInBackground(Habitat.java:1)
12-10 10:57:19.872: E/AndroidRuntime(301): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-10 10:57:19.872: E/AndroidRuntime(301): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-10 10:57:19.872: E/AndroidRuntime(301): ... 4 more