尝试从带有辅助类对象的Sqlite数据库获取数据时获取空指针异常

时间:2015-04-21 07:04:18

标签: android sqliteopenhelper csv-import

我想从.csv文件中获取数据,在应用程序启动时第一次将其存储到Sqlite数据库中。之后从Sqlite数据库获取数据并在可扩展列表视图中显示。但是当我获取空指针异常时我试图使用辅助类对象调用DatabaseHelper类的方法。

DatabaseHelper

  `public Cursor getExpandableData() {
    String query = "SELECT * FROM Category GROUP BY categorydesc";
        return db.rawQuery(query, null);
    }`

  public boolean insertContact(String[] rowData) {
    // TODO Auto-generated method stub
    db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("DistrictCd", rowData[0]);
    contentValues.put("ProspectId", rowData[1]);
    contentValues.put("categorydesc", rowData[2]);
    contentValues.put("BusinessName", rowData[3]);
    contentValues.put("ph_Phone", rowData[4]);
    contentValues.put("LandMark", rowData[5]);
    contentValues.put("CategoryId", rowData[6]);
    db.insert("Category", null, contentValues);
    // check for data is inserted into db or not
    db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("select * from Category", null);
    int noOfRecords = cursor.getCount();
    // if cursor holding some data then data is inserted successfully
    if (noOfRecords != 0) {
        return true;
    } else {
        return false;
    }
    }`

`        NearByApp只需要启动应用

  `public class NearByApp extends Application {
  public void onCreate() {SharedPreferences prefs =  
  PreferenceManager.getDefaultSharedPreferences(this);
  if (!prefs.getBoolean("firstTime", false)) {
        // run only once when app has been launched for the 1st time
        csvToSqlite();
        // mark first time has runned.
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("firstTime", true);
        editor.commit();
    }`

    `public void csvToSqlite() {
           //code for fetching data from .csv
            String[] RowData = reader.split(",");
            databaseHelper  = new DatabaseHelper(context);
            if (databaseHelper.insertContact(RowData) == true) {
                Log.i("inside csvToSqlite()", " data inserted     successfully");
            } else {
                Log.i("inside csvToSqlite()",
                        " data is not inserted into db");
            }`

MainActivity

         `protected void onCreate(Bundle savedInstanceState) {
        // Group data//
       databaseHelper = new DatabaseHelper(getApplicationContext());
       Cursor cursor=databaseHelper.getExpandableData();`
        }`

我在

获得了Exception
  

返回db.rawQuery(query,null);

线

  

04-21 14:25:49.810:E / AndroidRuntime(25473):引起:java.lang.NullPointerException   04-21 14:25:49.810:E / AndroidRuntime(25473):at com.example.nearbuy.database.DatabaseHelper.getExpandableData(DatabaseHelper.java:308)

1 个答案:

答案 0 :(得分:0)

下面:

mainActivity = new MainActivity();
mainActivity.csvToSqlite();

这些行导致问题,因为Activity未运行但尝试通过创建MainActivity的对象来调用方法。

因此,不要在csvToSqlite()类中的create MainActivity类调用csvToSqlite()方法中调用NearByApp来执行方法