0

我正在解析 json 数据,并尝试将该数据放入数据库中,我在我的 DownloadJson 类中获取了我的 dbadapter 实例并传递了值,但它给出了空指针异常。下面是我的代码

public class DownloadJSON extends AsyncTask<Void, Void, Void> {

    public static final String UNIVERSITY_NAMES = "name";
    public static final String UNIVERSITY_URL = "url";


    private static MyDBAdapter dbHelper;

    String fileName = "json.txt";


    ArrayList<HashMap<String, String>> arraylist;

    private Context context;




        public DownloadJSON(Context context){
            this.context = context;
        }


    @Override
    protected Void doInBackground(Void... params) {
        //startWebServiceForExcelData();
         readFileFromAssets(fileName,context);
        return null;

}

public static String readFileFromAssets(String fileName, Context context) {
     AssetManager assetManager = context.getAssets();
     InputStream is = null;
        try {
            is = assetManager.open(fileName);

            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String text = new String(buffer);
System.out.println("tex===========t"+ text);

parseJson(text);
            return text;

        } catch (IOException e) {
            throw new RuntimeException(e);}}

private static void parseJson(String text) {

     ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
     try {
         JSONObject mainObject = new JSONObject(text);
         JSONObject uniObject = mainObject.getJSONObject("university");
         String  uniName = uniObject.getString(UNIVERSITY_NAMES);
         String uniURL = uniObject.getString(UNIVERSITY_URL);

         System.out.println("uniName============="+uniName);
         System.out.println("uniURL============="+uniURL);

         dbHelper.saveCategoryRecord(new University(uniName,uniURL));// showing  error at this line

         HashMap<String, String> map = new HashMap<String, String>();
         // adding each child node to HashMap key => value
         map.put(UNIVERSITY_NAMES, uniName);
         map.put(UNIVERSITY_URL, uniURL);       
         // adding HashList to ArrayList
         contactList.add(map);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }}}

Belos 是我的数据库适配器中的方法

  public void saveCategoryRecord(University university) {

        String query = "insert into"+ SQLITE_TABLE2 + "values ( ?, ?,)";
                SQLiteStatement stmt = mDb.compileStatement(query);// error at this line
                stmt.bindString(1, university.getName());
                stmt.bindString(2, university.getUrl());
                stmt.execute();
                System.out.println("bindString=========="+ stmt);
    }

以下是我的日志跟踪

   09-24 13:23:52.875: E/AndroidRuntime(30727): Caused by: java.lang.NullPointerException
09-24 13:23:52.875: E/AndroidRuntime(30727):    at com.markupartist.android.actionbar.example.MyDBAdapter.saveCategoryRecord(MyDBAdapter.java:206)
09-24 13:23:52.875: E/AndroidRuntime(30727):    at com.markupartist.android.actionbar.example.DownloadJSON.parseJson(DownloadJSON.java:124)
09-24 13:23:52.875: E/AndroidRuntime(30727):    at com.markupartist.android.actionbar.example.DownloadJSON.readFileFromAssets(DownloadJSON.java:101)
09-24 13:23:52.875: E/AndroidRuntime(30727):    at com.markupartist.android.actionbar.example.DownloadJSON.doInBackground(DownloadJSON.java:42)
09-24 13:23:52.875: E/AndroidRuntime(30727):    at com.markupartist.android.actionbar.example.DownloadJSON.doInBackground(DownloadJSON.java:1)
09-24 13:23:52.875: E/AndroidRuntime(30727):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-24 13:23:52.875: E/AndroidRuntime(30727):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-24 13:23:52.875: E/AndroidRuntime(30727):    ... 5 more

我在哪里做错了?

4

3 回答 3

1

dbHelper 永远不会被初始化。你应该在dbHelper = new MyDBAdapter(...)某个地方有或类似的东西......

于 2013-09-24T07:49:24.913 回答
0

您忘记初始化数据库适配器类,并且直接在parseJson()方法中使用它。

在您的构造函数中初始化您的数据库适配器,如下所示。

public DownloadJSON(Context context){
    this.context = context;
    dbHelper=new MyDBAdapter();
}

编辑:

在您的saveCategoryName()方法中,您的查询中有额外的逗号,只需,从查询中删除最后一个并将其编写如下:

String query = "insert into"+ SQLITE_TABLE2 + "values ( ?,?)";
于 2013-09-24T07:56:13.237 回答
0

你确定上下文在这里不为空吗

AssetManager assetManager = context.getAssets();

与 JSONException 和 IOException 一起处理顶级异常

 } catch (JSONException e) {

 } catch (IOException e) {

此外,您需要处理此方法可能引发的异常:

readFileFromAssets(fileName,context);

我希望这会有所帮助,请随时提出更多问题。

于 2013-09-24T07:57:16.203 回答