将json数组插入sqlite数据库时出现Null指针异常

时间:2011-12-07 06:14:58

标签: java android json sqlite nullpointerexception

    12-07 11:25:34.290: E/AndroidRuntime(9177): FATAL EXCEPTION: AsyncTask #2
12-07 11:25:34.290: E/AndroidRuntime(9177): java.lang.RuntimeException: An error occured while executing doInBackground()
12-07 11:25:34.290: E/AndroidRuntime(9177):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-07 11:25:34.290: E/AndroidRuntime(9177):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-07 11:25:34.290: E/AndroidRuntime(9177):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-07 11:25:34.290: E/AndroidRuntime(9177):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-07 11:25:34.290: E/AndroidRuntime(9177):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-07 11:25:34.290: E/AndroidRuntime(9177):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-07 11:25:34.290: E/AndroidRuntime(9177):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-07 11:25:34.290: E/AndroidRuntime(9177):     at java.lang.Thread.run(Thread.java:1096)
12-07 11:25:34.290: E/AndroidRuntime(9177): Caused by: java.lang.NullPointerException
12-07 11:25:34.290: E/AndroidRuntime(9177):     at com.UserLogin.Onaroundyou$getOnaroundyou.doInBackground(Onaroundyou.java:198)
12-07 11:25:34.290: E/AndroidRuntime(9177):     at com.UserLogin.Onaroundyou$getOnaroundyou.doInBackground(Onaroundyou.java:1)
12-07 11:25:34.290: E/AndroidRuntime(9177):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-07 11:25:34.290: E/AndroidRuntime(9177):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-07 11:25:34.290: E/AndroidRuntime(9177):     ... 4 more

当我试图将json数组插入我的sqlite数据库时,我得到了这个Nullpoint异常错误。 在Lline 198上 我们从jsonarray获取值将其添加到内容值。

直到这行我得到了json响应的值我在日志中打印了键和值。检查我是否获得任何空值。但价值观正在变好,我能够在日志中看到这些。当我的deguger进入下面的线时,我得到了错误。请帮助一次。

Evalues.put(InsertData.MyName,JSonArray1.getJSONObject(i).getString("MyName"));

提前致谢。

        private TextView tv;
        String str, Username, Eresult,Password;
        ProgressDialog dialog1;
        DatabaseHelper InsertEData;
        String attributeId ;
        Context mContext = this;
        SQLiteDatabase Edb ;
        JSONObject MyUserJsonObject;

    protected Void doInBackground(String... params) {
        // TODO Auto-generated method stub
                HttpClient httpclient = new DefaultHttpClient();
                // Dh.getWritableDatabase();

                String text1 = getString(R.string.site_name);
                String Url=text1+"Details";

                HttpPost httppost = new HttpPost(Url);


                try {
                HttpResponse responseGet = httpclient.execute(httppost);
                HttpEntity entity = responseGet.getEntity();
                if (entity != null) {
                    InputStream inputStreamResponse = entity.getContent();
                    Eresult = convertStreamToString(inputStreamResponse);
                    System.out.println(Eresult);
                    Edb= InsertEData.getWritableDatabase();

                    ContentValues Evalues = new ContentValues();
                    Evalues.clear();
                    MyUserJsonObject = new JSONObject(Eresult);

                    JSONObject menuObject = MyUserJsonObject.getJSONObject("UserDetails");
                    attributeId = menuObject.getString("login");

                    System.out.println("attribute  "+attributeId);
                    if (attributeId.equals("success")) {
                        JSONObject EObject = menuObject.getJSONObject("Ulist");
                        JSONArray EArray = EObject.getJSONArray("List");
                        System.out.println(EArray);
                        System.out.println(EArray.length());
                        for (int i = 0; i < EArray.length(); i++) {                             
                            //System.out.println(EArray.getJSONObject(0).getString("Name").toString());

                            Evalues.put(InsertEData.EName, EArray.getJSONObject(i).getString("MyName"));
                            Evalues.put(InsertEData.ELocation, EArray.getJSONObject(i).getString("Location"));
                            Evalues.put(InsertEData.ETotal, EArray.getJSONObject(i).getString("Total"));
                            Evalues.put(InsertEData.Eimage, EArray.getJSONObject(i).getString("Image"));
                            Edb.insert(InsertEData.ESLIST, null, Evalues );

                        }
                        Evalues.clear();

                        inputStreamResponse.close();

                    }else {
                        System.out.println("Unable to load page - " + responseGet.getStatusLine());
                    }
                }
                else
                    System.out.println("Entity Null");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                System.out.println("Network Error");
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }

            return null;}

2 个答案:

答案 0 :(得分:1)

您的数组似乎返回null值。

请保持日志,检查和控制台。

答案 1 :(得分:0)

  

“任何人都可以知道为什么会发生这种情况。”

简短的回答是:因为某些对象不应该是null。可能的罪魁祸首可能是InsertDataJSonArray1JSonArray1.getJSONObject(i)的结果。

如果没有更多细节,很难找出确切的原因。我建议您断开代码以找到问题。