尝试将已解析的数据插入数据库时​​出现Nullpointer异常

时间:2013-09-24 07:47:01

标签: android database android-asynctask nullpointerexception

我正在解析json数据,并试图将数据放入数据库,我在我的DownloadJson类中获取了我的dbadapter实例并传递了值,但它给出了nullpointer exception.below是我的代码

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

我做错了什么?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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

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

修改

在您的saveCategoryName()方法中,您的查询中有额外的逗号,只需从查询中删除最后一个,,然后按如下所示进行编写:

String query = "insert into"+ SQLITE_TABLE2 + "values ( ?,?)";

答案 2 :(得分:0)

你确定上下文不是空

AssetManager assetManager = context.getAssets();

处理顶级异常以及JSONException和IOException

 } catch (JSONException e) {

 } catch (IOException e) {

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

readFileFromAssets(fileName,context);

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