This is my DatabaseHelper class
public class DatabaseHelper extends SQLiteOpenHelper
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "MyAccountsDB.db";
public static final String TABLE_Accounts = "Accounts";
public static final String COLUMN_AccountID= "AccountID";
public static final String COLUMN_WebSite= "WebSite";
public static final String COLUMN_Email= "Email";
public static final String COLUMN_UserName = "UserName";
public static final String COLUMN_Password= "Password";
private static final String TEXT_TYPE = " TEXT";
private static final String INT_TYPE = " INTEGER";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_TABLE_Accounts =
"CREATE TABLE " + TABLE_Accounts +
" (" +
COLUMN_AccountID + INT_TYPE +" PRIMARY KEY AUTOINCREMENT NOT NULL"+ COMMA_SEP +
COLUMN_WebSite + TEXT_TYPE + COMMA_SEP +
COLUMN_Email + TEXT_TYPE + COMMA_SEP +
COLUMN_UserName + TEXT_TYPE + COMMA_SEP +
COLUMN_Password+ TEXT_TYPE + ")";
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(SQL_CREATE_TABLE_Accounts);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Accounts);
onCreate(db);
}
public void CreateAccount(Account acc)
{
SQLiteDatabase db = getWritableDatabase();
String insertAccount = "Insert into Accounts (WebSite,Email,UserName,Password) values" +
"('" + acc.getWebSite() + "','" + acc.geteMail() + "','" + acc.getUserName() + "','" + acc.getPassWord() + "')";
db.execSQL(insertAccount);
}
public ArrayList<Account> getAllAccounts()
{
String query = "SELECT AccountID , WebSite FROM Accounts";
ArrayList<Account> Accounts = new ArrayList<Account>();
SQLiteDatabase database = getReadableDatabase();//when its called here no problem and load all the accounts the way i want
Cursor c = database.rawQuery(query, null);
if (c != null)
{
while (c.moveToNext())
{
Account acc = new Account();
acc.setAccountID(c.getInt(0));
acc.setWebSite(c.getString(1));
Accounts.add(acc);
}
}
return Accounts;
}
public Account getAccountByID(int id)
{
Account acc = new Account();
String query = "SELECT WebSite , eMail , UserName , Password from Accounts where AccountID = " + id;
SQLiteDatabase database = getReadableDatabase();//but when i call it here following exception thrown
Cursor c = database.rawQuery(query, null);
if (c != null)
{
while (c.moveToFirst())
{
acc.setAccountID(c.getInt(0));
acc.setWebSite(c.getString(1));
acc.seteMail(c.getString(2));
acc.setUserName(c.getString(3));
acc.setPassWord(c.getString(4));
}
}
return acc;
}
NullPointerException :
Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
I have another application with the same methods and functions syntax and it's working with no problems