插入数据库时​​出错

时间:2014-07-15 07:00:23

标签: java android database sqlite nullpointerexception

当我尝试注册用户并将其详细信息保存到数据库时,有人可以解释为什么我收到NullPointerException吗?

这是xml文件,只询问用户的详细信息

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/eT_R_userName"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ems="10"
        android:hint="User Name"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/eT_R_email"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ems="10"
        android:hint="Email"
        android:inputType="textEmailAddress" />

    <EditText
        android:id="@+id/eT_R_password"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btn_R_register"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:onClick="Login"
        android:text="Register" />

</LinearLayout>

onClick Listener

Button btn_register = (Button)findViewById(R.id.btn_R_register);

btn_register.setOnClickListener(new View.OnClickListener() 
{

    @Override
    public void onClick(View v) 
    {

        //insert user profile data into database
        final String Name = user_name.getText().toString();
        final String Email = email.getText().toString();
        final String Password = password.getText().toString();

        //Debug
        System.out.println(">>>onClick1---> Register User\nName: " + Name + "\nEmail: " + Email + "\nPassword: " + Password);

        //Method
        insert(Name, Email, Password);
    }
});

插入数据的方法,此处发生NullPointerException

    public void insert(String Name, String Email, String Password)
    {
        //Debug
        System.out.println(">>>Method---> insert user data into Database\n" + Name + "\n" + Email + "\n" + Password);

        db.insertData(Name, Email, Password);
}

数据库内容如下所示

public static final String USER_ID = "email";
public static final String USER_NAME = "user_name";
public static final String PASSWORD = "password";

public static final String DATABASE_NAME = "molo.db";
public static final String DATABASE_TABLE = "user_profile";
private static final int DATABASE_VERSION = 1;

//SQL to create new database
private static final String DATABASE_CREATE = 
        "create table " + DATABASE_TABLE +
        " (" +
        USER_ID + " text not null, " +
        USER_NAME + " text not null, " +
        PASSWORD + " text not null);";

以及

public void insertData(String user_ID, String user_Name, String Password)
{
    // Put these elements in the User Profile table
    ContentValues element = new ContentValues();
    element.put(com.example.molo.UserDB.USER_ID, user_ID);
    element.put(com.example.molo.UserDB.USER_NAME, user_Name);
    element.put(com.example.molo.UserDB.PASSWORD, Password);
    database.insert(com.example.molo.UserDB.DATABASE_TABLE, null, element);
}

1 个答案:

答案 0 :(得分:3)

这个地方的db对象为null

db.insertData(Name, Email, Password);

您获得NullPointerException,因为您正在尝试对null的对象执行操作。在insert()方法中使用它之前,请确保正确初始化它。