-6

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

0

I believe you need to create the database helper differently.

For example,

public class MainActivity extends Activity {

    private DatabaseHelper db; // Don't initialize here

    @Override
    protected void onCreate(Bundle b) {
        ...
        db = new DatabaseHelper(this); // Do it here
    }
}

And that should fix the current error if you use the same db variable throughout the activity, but you still have other issues in the SQL queries that don't match your table definition.

You also don't want to use while (c.moveToFirst()) unless you want that loop to never end.

public Account getAccountByID(int id)
{
    Account acc  = new Account();
    SQLiteDatabase database = getReadableDatabase();

    Cursor c = database.query(TABLE_Accounts, 
            { COLUMN_WebSite , COLUMN_Email , COLUMN_UserName , COLUMN_Password }, 
            COLUMN_AccountID + "=?", 
            { String.valueOf(id) }, 
            null, null, null, null);
        );    

    try {
        if (c != null && c.moveToFirst())
        {
            acc.setAccountID(id);
            acc.setWebSite(c.getString(0));
            acc.seteMail(c.getString(1));
            acc.setUserName(c.getString(2));
            acc.setPassWord(c.getString(3));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (c != null) c.close();
    }
    return acc;
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Thank you all...i've found the answer....i was calling the getAccountById method from a fragment...so i must re-pass the Context again to it

UpdateAccountFragment a = new UpdateAccountFragment(accountID,getActivity());

and the constructor of the UpdateAccountFragment look like

 public UpdateAccountFragment (int accountID , Context context)
{
    this.context = context;
    this.accountID = accountID;
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
-1

The problem is a in your Query:

String query = "SELECT WebSite , eMail , UserName , Password from Accounts where AccountID = " + id;

You have used eMail instead of Email. There is no column in your Accounts table named eMail. Also change from to FROM and where clause to WHERE.

Change your Query as below:

String query = "SELECT WebSite , Email , UserName , Password FROM Accounts WHERE AccountID = " + id;

# Create instance of DatabaseHelper from your Activity or Fragment like below:

From Activity:

public class MainActivity extends AppCompatActivity {

    DatabaseHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = new DatabaseHelper(this); // Context is this

        ...............
        ....................
    }

}

From Fragment:

public class UpdateAccountFragment extends Fragment {

    DatabaseHelper db;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        db = new DatabaseHelper(getActivity()); // Context is getActivity()

        ...............
        ....................
    }
}

Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61