在向数据库添加数据时获取空指针异常

时间:2013-10-15 10:30:59

标签: android sqlite

在向数据库添加数据时,我遇到了一个nullpointer异常。 我的java文件粘贴在这里,

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);
    findViewById();

    btnSave.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if(!etPassword.getText().toString().equals(etConfPassword.getText().toString())){
                 displayToast("Please confirm Password");
            }else if(etUserName.getText().toString().equals("")){
                displayToast("Please enter Login ID");
            }else if(etPassword.getText().toString().equals("")){
                displayToast("Please enter Password");
            }else if(isExisting(etFirstName.getText().toString(), "loginId", "UserDetails")){
                displayToast("This User already exist");
            }else{
                    handleDB();
                    finish();
                    startActivity(new Intent(getApplicationContext(), Replay.class));   
            }
       }    
   });    
}
private void handleDB() {
    try {
        ContentValues contentValues = new ContentValues();
        contentValues.put("firstName", etFirstName.getText().toString());
        contentValues.put("lastName", etLastName.getText().toString());
        contentValues.put("loginId", etUserName.getText().toString());
        contentValues.put("password", etPassword.getText().toString());
        contentValues.put("cofmPassword", etConfPassword.getText().toString());
        contentValues.put("emailAddress", etEmailAddress.getText().toString());
        Replay.database.insert("UserDetails", null, contentValues);
    } catch(SQLiteException se ){
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");}}
        private void displayToast(String msg){
        Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
    }
private void findViewById(){
    etFirstName = (EditText) findViewById(R.id.FName);
    etLastName = (EditText) findViewById(R.id.LName);
    etUserName = (EditText) findViewById(R.id.LoginId);
    etPassword = (EditText) findViewById(R.id.Password);
    etConfPassword = (EditText) findViewById(R.id.ConfPassword);
    etEmailAddress = (EditText) findViewById(R.id.Email);
    btnSave = (Button)findViewById(R.id.btnSave);
}};

,错误日志为

10-15 08:51:55.932: E/AndroidRuntime(606): FATAL EXCEPTION: main
10-15 08:51:55.932: E/AndroidRuntime(606): java.lang.NullPointerException
10-15 08:51:55.932: E/AndroidRuntime(606):  at com.GenioCode.replay.Register$1.onClick(Register.java:38)
10-15 08:51:55.932: E/AndroidRuntime(606):  at android.view.View.performClick(View.java:4204)
10-15 08:51:55.932: E/AndroidRuntime(606):  at android.view.View$PerformClick.run(View.java:17355)

创建编译数据库后没有任何内容。 请帮我识别错误..

1 个答案:

答案 0 :(得分:0)

在这段代码中:

if(!etPassword.getText().toString().equals(etConfPassword.getText().toString())){
                        displayToast("Please confirm Password");
                    }else if(etUserName.getText().toString().equals("")){
                        displayToast("Please enter Login ID");
}else if(etPassword.getText().toString().equals("")){
                        displayToast("Please enter Password");
}else if(isExisting(etFirstName.getText().toString(), "loginId", "UserDetails")){
                        displayToast("This User already exist");

您未能事先检查是否:

(etPassword != null) && (etConfPassword != null) && 
(etUserName != null) && (etFirstName != null);

如果这些字段中的任何一个为空,您将获得一个nullpointer异常 您必须首先检查这些条件或通过其他一些努力来确保在达到此点之前,所有相关变量都保证不为空。

简单地将它们初始化为

String etPassword = "";

会做到这一点 这是关于Java的烦人事情之一。在许多其他语言中,字符串以空字符串开头,而不是空字符串。