2013-09-24 104 views
0

我解析json数据,并试图将该数据放入数据库中,我拿我的dbadapter在我的DownloadJson类的实例,并传递值,但它给予空指针exception.below是我的代码Nullpointer异常,当试图插入分析数据到数据库

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是我在我的数据库适配器methos

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 

我在哪里做错了?

+0

你的json响应是什么? – GrIsHu

+0

我忘了初始化dbadapter,现在我做了它的工作..thx – teekib

+0

检查我更新的答案。 – GrIsHu

回答

0

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

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

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

编辑:

在你saveCategoryName()方法有在查询额外的逗号只是删除从查询的最后,和它下面写:

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

嗨,我现在做了saveCategoryRecord()方法中的错误 – teekib

+0

你正在得到什么错误?请张贴它。 – GrIsHu

+0

@teekib检查我更新的答案。 – GrIsHu

1

dbHelper从不初始化。你应该有dbHelper = new MyDBAdapter(...)或其他类似的地方...

+0

确切地说..你是对的...我初始化了dbHelper = new MyDBAdapter(context);它的工作 – teekib

0

确定上下文不为空

AssetManager assetManager = context.getAssets(); 

处理顶层异常与JSONException和IOException异常沿

} catch (JSONException e) { 

} catch (IOException e) { 

此外,你需要处理,可以通过此方法抛出的异常:

readFileFromAssets(fileName,context); 

我希望这可以帮助,随意提出更多问题。