简体   繁体   中英

How to eliminate null pointer exception on OpenOrCreateDatabase(****) which is called by context object of my Registration class?

I am Working on simple login application in which user is first shown login screen, in login screen I have two edittext to let the user enter usrname and password. If user presses the register button then the application takes the user to registration page where the registration details in four fields named(uname,pwd, Email Add,Postal Add) entered by user should be inserted in database.

In my Registration class I have created object of my DatabaseOperation Class by passing the context of Registration class, then the constructor of DatabaseOperation is called as the constructor is called context object is copied in another object named mcontext . After this method named Create_tbl to create the table is callled.

In this method I called OpenOrCreateDatabase() Method with mcontext object as.......

public void Create_Tbl()
    {
    db = mcontext.openOrCreateDatabase("Register.db", SQLiteDatabase.CREATE_IF_NECESSARY,null);
    CREATE_TABLE_REGISTER = "CREATE TABLE IF NOT EXISTS "+table_name+"(u_id INTEGER PRIMARY KEY AUTOINCREMENT,u_name TEXT,u_eadd TEXT,u_pwd TEXT,u_padd TEXT)";
    db.execSQL(CREATE_TABLE_REGISTER);
    }

My Question is : Why I am getting NullPointerException at openOrCreateDatabase() .

In addition I am getting NullPointerException at two other places

  1. Create_Tbl();
  2. DatabaseOperation dos = new DatabaseOperation(Registration.this);

There may be many issues, with this problem, but most striking is mContext, please debug your code and check it is not null. and call method openOrCreateDatabase (String path, SQLiteDatabase.CursorFactory factory) instead of openOrCreateDatabase (String path, SQLiteDatabase.CursorFactory factory, null).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM