在android中调用insertData()时出现NullPointerException

时间:2018-04-17 04:17:24

标签: sqlite android-sqlite sql-insert android-databinding android-database

我目前正在研究android速记项目, 但在注册时发生了错误

"java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.example.devd.stangnograpyonimage.DBHelper.insertData(java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference"

DataBaseHelper.java

public static final String DATABASE_NAME = "STAGNODB";
public static final String TABLE_NAME = "Satgno_Table";
//public static final String COLUUMN_ID  = "ID";
public static final String COLUUMN_USERNAME  = "UserName";
public static final String COLUUMN_PASSWORD = "Password";
public static final String COLUUMN_NAME = "Name";
public static final String COLUUMN_EMAIL = "Email";


public DBHelper(Context context) {

    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    try {
        db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + COLUUMN_USERNAME + " VARCHAR(50),"
                + COLUUMN_PASSWORD + " VARCHAR(50),"
                + COLUUMN_NAME + " VARCHAR(50),"
                + COLUUMN_EMAIL + " VARCHAR(50));", null);
    }catch (Exception e){
        Log.e("SQL Table Creation ", "Table Not Created");
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}

public  boolean insertData(String userName, String password, String name, String email){
    SQLiteDatabase mydb = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(COLUUMN_USERNAME, userName);
    cv.put(COLUUMN_PASSWORD, password);
    cv.put(COLUUMN_NAME, name);
    cv.put(COLUUMN_EMAIL, email);
    long result = mydb.insert(TABLE_NAME, null, cv);

    if(result == -1){
        return  false;
    }
    else {
        return  true;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    DBHelper myDB;
    EditText etUserName, etPassword, etName, etEmail;
    Button btnRegister, btnLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etUserName = (EditText) findViewById(R.id.etUserName);
        etPassword = (EditText) findViewById(R.id.etPassword);
        etName = (EditText) findViewById(R.id.etName);
        etEmail = (EditText) findViewById(R.id.etEmail);
        btnRegister = (Button) findViewById(R.id.btnRegister);
        btnLogin = (Button) findViewById(R.id.btnLogin);
        register();
    }
    public void register() {
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isInserted = myDB.insertData(etUserName.getText().toString(), etPassword.getText().toString(), etName.getText().toString(), etEmail.getText().toString());
                if(isInserted==true){
                    Toast.makeText(MainActivity.this,"Data Inserted", Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(MainActivity.this,"Data not Inserted", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

2 个答案:

答案 0 :(得分:2)

在您的活动类中,您尚未初始化变量DBHelper myDB。因此,当您的活动调用时

myDB.insertData(etUserName.getText().toString(), ...

您收到NullPointerException,因为myDB为空。要解决此问题,请在活动的onCreate方法中创建对象

myDB = new DBHelper(this);

答案 1 :(得分:1)

您声明但未实例化 myDB ,因此它为null。

    DBHelper myDB; 

只需声明 myDB ,因此此时它为空,因为没有为变量分配任何内容。

您需要添加以下内容: -

    myDB = new DBHelper(this);

onCreate MainActivity方法。

e.g。 : -

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    etUserName = (EditText) findViewById(R.id.etUserName);
    etPassword = (EditText) findViewById(R.id.etPassword);
    etName = (EditText) findViewById(R.id.etName);
    etEmail = (EditText) findViewById(R.id.etEmail);
    btnRegister = (Button) findViewById(R.id.btnRegister);
    btnLogin = (Button) findViewById(R.id.btnLogin);
    myDB = new DBHelper(this); //<<<< ADDED
    register();
}