1

In the development of Android applications I use Android SQLiteAssetHelper. The problem is that occasionally when accessing the database I get an error to the object NullPointerException in object DBQuery

public class DBQuery extends MyDatabase {
  private static MyDatabase dataBaseHelper;

  public DBQuery(Context context) {
        super(context);
        dataBaseHelper = new MyDatabase(context);
  }

  public static void closed() {
        dataBaseHelper.close();
  }

  public static int getFDay(int year) {
    Cursor cursor = database.rawQuery("SELECT offset FROM china_years WHERE year = ?",
        new String[] { Integer.toString(year) });

    cursor.moveToFirst();
    String yearS;
    try {
      yearS = cursor.getString(0);
    } catch (Exception e) {
      yearS = "0";
      e.printStackTrace();
    }
    cursor.close();

    return Integer.parseInt(yearS);
  }

  /* other queries... */    
}

MyDatabase class:

public class MyDatabase extends SQLiteAssetHelper {

   private static final String DATABASE_NAME = "db.sqlite";
   private static final int DATABASE_VERSION = 2;      
   public static SQLiteDatabase database;

   public MyDatabase(Context context) {
     super(context, DATABASE_NAME, null, DATABASE_VERSION);

     database = getReadableDatabase();
   }
}

UDP: Sorry, null object is 'database' in DBQuery class.

bloodvlad
  • 13
  • 5

2 Answers2

0

Add Database name Like this & check that database is create or not

 private static final String DATABASE_NAME = "Test";

data/data/YOUR_APP_PACKAGE_NAME Location

Dixit Patel
  • 3,190
  • 1
  • 24
  • 36
-1

You have to open your data base first. Than only you can perform operation on that.

Manoj Fegde
  • 4,786
  • 15
  • 50
  • 95