我的数据库的密码,我正在尝试插入记录值,但是在创建记录和插入值时出现空指针异常,下面是我的 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)