0

Cipher for my database , i am trying to insert the record values but i am getting nullpointer exception while creating the record and inserting the values, below is my Databasehelper code

public class DatabaseHelper extends SQLiteOpenHelper {
             public static final String KEY_ROWID = "_id";
             public static final String KEY_CODE = "code";
             public static final String KEY_NAME = "name";
             public static final String KEY_CONTINENT = "continent";
             public static final String KEY_REGION = "region";
             public static final String KEY_AREA = "area";


             private static final String TAG = "CountriesDbAdapter";
             private DatabaseHelper mDbHelper;
             private SQLiteDatabase mDb;
             String password = "foo123";

             private static final String DATABASE_NAME = "salesreports";
             private static final String SQLITE_TABLE = "salesandreports";
             private static final String SQLITE_TABLE1 = "salescharts";
             private static final int DATABASE_VERSION = 1;



             private static final String DATABASE_CREATE =
              "CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
              KEY_ROWID + " integer PRIMARY KEY autoincrement," +
              KEY_CODE + " INTEGER," +
              KEY_NAME + " INTEGER," +
              KEY_CONTINENT + " INTEGER," +
              KEY_REGION + " INTEGER," +
              KEY_AREA + " INTEGER" +
             ")";    


        @Override
        public void onCreate(SQLiteDatabase db) {
             db.execSQL(DATABASE_CREATE);



        }


        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);

               onCreate(db);

        }


         public long createsalesCountry(String code, String name, 
           String continent, String region,String area) {

          ContentValues initialValues = new ContentValues();
          initialValues.put(KEY_CODE, code);
          initialValues.put(KEY_NAME, name);
          initialValues.put(KEY_CONTINENT, continent);
          initialValues.put(KEY_REGION, region);
          initialValues.put(KEY_AREA, area);

          return mDb.insert(SQLITE_TABLE, null, initialValues);

         }


         public boolean deleteAllCountries() {

          int doneDelete = 0;
          doneDelete = mDb.delete(SQLITE_TABLE, null , null);
          Log.w(TAG, Integer.toString(doneDelete));
          return doneDelete > 0;

         }

         public Cursor fetchCountriesByName(String inputText) throws SQLException {
          Log.w(TAG, inputText);
          Cursor mCursor = null;
          if (inputText == null  ||  inputText.length () == 0)  {
           mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
             KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION,KEY_AREA}, 
             null, null, null, null, null,null);

          }
          else {
           mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
             KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION,KEY_AREA}, 
             KEY_NAME + " like '%" + inputText + "%'", null,
             null, null, null, null);
          }
          if (mCursor != null) {
           mCursor.moveToFirst();
          }
          return mCursor;

         }

         public Cursor fetchAllCountries() {

          Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
            KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION,KEY_AREA}, 
            null, null, null, null, null,null);

          if (mCursor != null) {
           mCursor.moveToFirst();
          }
          return mCursor;
         }




         public void insertsalesvalues() {

          createCountry("1","1","1","1","1");}}

and below is my activity

 public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            LayoutToDisplayChart=(LinearLayout)findViewById(R.id.relative_layout1);
            SQLiteDatabase.loadLibs(this);

            String password = "foo123";
            dbHelper = new DatabaseHelper(this);
            SQLiteDatabase db = dbHelper.getWritableDatabase(password);
            //dbHelper.insertsalesvalues();
            dbHelper.insertSomeCountries();
            Intent achartIntent = new aChart_Example().execute(MainActivity.this,LayoutToDisplayChart);

Below is my trace

Caused by: java.lang.NullPointerException
09-17 11:02:01.500: E/AndroidRuntime(27757):    at com.example.mychartapp.DatabaseHelper.createsalesCountry(DatabaseHelper.java:113)
09-17 11:02:01.500: E/AndroidRuntime(27757):    at com.example.mychartapp.DatabaseHelper.insertsalesvalues(DatabaseHelper.java:198)
09-17 11:02:01.500: E/AndroidRuntime(27757):    at com.example.mychartapp.MainActivity.onCreate(MainActivity.java:26)
teekib
  • 2,821
  • 10
  • 39
  • 75

2 Answers2

0

Adding this line solved my problem **SQLiteDatabase mDb = this.getWritableDatabase(password);**

  public long createsalesCountry(String code, String name, 
               String continent, String region,String area) {
                 **SQLiteDatabase mDb = this.getWritableDatabase(password);**
              ContentValues initialValues = new ContentValues();
              initialValues.put(KEY_CODE, code);
              initialValues.put(KEY_NAME, name);
              initialValues.put(KEY_CONTINENT, continent);
              initialValues.put(KEY_REGION, region);
              initialValues.put(KEY_AREA, area);

              return mDb.insert(SQLITE_TABLE, null, initialValues);

             }


    public void insertSalesvalues() {
                 **SQLiteDatabase mDb = this.getWritableDatabase(password);**
              createCountry("1","1","1","1","1");



             }
teekib
  • 2,821
  • 10
  • 39
  • 75
0

First thing you have to do is, check where you have your database file, whether in your assets, or /data/data/your package name/your_application_name. If it is in your assets, you have to copy it to your /data/data/your package name/ folder. Then, after copying your DB file, save that DB file to your PC, check the structure of the database file, the data present in it(if any). You are telling that, you could not insert or retrieve any data right? By checking your database file in this way, you can understand your mistake in the back-end. But, to my knowledge, I did not find any mistake in your code.

nki
  • 192
  • 3
  • 17