简体   繁体   English

如何消除OpenOrCreateDatabase(****)上的空指针异常,该异常由我的Registration类的上下文对象调用?

[英]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. 我正在使用一个简单的登录应用程序,在该应用程序中首先显示用户登录屏幕,在登录屏幕中,我有两个edittext让用户输入usrname和密码。 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. 如果用户按下注册按钮,则应用程序会将用户带到注册页面,在该页面中,应将用户输入的四个字段(uname,pwd,Email Add,Postal Add)中的注册详细信息插入数据库。

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 . 在我的Registration类中,我通过传递Registration类的上下文创建了DatabaseOperation类的对象,然后调用DatabaseOperation的构造函数,因为该构造函数被称为上下文对象,该对象被复制到另一个名为mcontext对象中。 After this method named Create_tbl to create the table is callled. 在此名为Create_tbl方法用于创建表之后,将被Create_tbl

In this method I called OpenOrCreateDatabase() Method with mcontext object as....... 在此方法中,我使用mcontext对象as .......调用OpenOrCreateDatabase()方法。

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() . 我的问题是:为什么我在openOrCreateDatabase()处收到NullPointerException

In addition I am getting NullPointerException at two other places 另外我在另外两个地方也得到了NullPointerException

  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. 这个问题可能有很多问题,但最引人注目的是mContext,请调试您的代码并检查它是否不为null。 and call method openOrCreateDatabase (String path, SQLiteDatabase.CursorFactory factory) instead of openOrCreateDatabase (String path, SQLiteDatabase.CursorFactory factory, null). 然后调用方法openOrCreateDatabase(字符串路径,SQLiteDatabase.CursorFactory工厂),而不是openOrCreateDatabase(字符串路径,SQLiteDatabase.CursorFactory工厂,null)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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