-1

请帮助我为什么会收到此错误。我正在使用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
4

2 回答 2

0

我认为这是空的

HabitatPostsArray.getJSONObject(i).getString("userpostimage")

您是否尝试打印该值?检查该行之前的值,基本检查如下:

JSONObject jo = HabitatPostsArray.getJSONObject(i);

if (jo == null)
    Log.d("tag", "json obj null "+i);
if (!jo.has("userpostimage"))
    Log.d("tag", "key not present, you are not parsing correctly the json");

String s = jo.getString("userpostimage");
if (s == null)
    Log.d("tag", "userpostimage null");
于 2011-12-10T06:16:16.157 回答
0

尝试记录你的 JsonObject [jo],你在那里得到值吗?JSONObject jo = HabitatPostsArray.getJSONObject(i);

if (jo == null)
    Log.d("tag", "json obj null "+i);
else 
    Log.d("tag", "json object at index" + i + " is : " + jo.toString());
于 2011-12-10T08:08:58.420 回答