0

我的数据库的密码,我正在尝试插入记录值,但是在创建记录和插入值时出现空指针异常,下面是我的 Databasehelper 代码

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");}}

以下是我的活动

 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);

下面是我的踪迹

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)
4

2 回答 2

0

添加这一行解决了我的问题 **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");



             }
于 2013-09-17T07:10:36.127 回答
0

您要做的第一件事是检查数据库文件的位置,无论是在资产中,还是在 /data/data/your package name/your_application_name 中。如果它在您的资产中,您必须将其复制到您的 /data/data/your package name/ 文件夹中。然后,在复制您的 DB 文件后,将该 DB 文件保存到您的 PC,检查数据库文件的结构,其中存在的数据(如果有)。您是说,您无法插入或检索任何数据,对吗?通过这种方式检查你的数据库文件,你可以在后端理解你的错误。但是,据我所知,我在您的代码中没有发现任何错误。

于 2013-09-17T10:20:32.580 回答