SQLite中的空指针异常

时间:2013-11-26 12:59:32

标签: java android sqlite

我正在为sqlite数据库中的插入记录实现app。但是当我运行应用程序时,我在以下行得到了空指针异常:

String userPin=editTextPin.getText().toString();
String userConfirmPin=editTextConfirmPin.getText().toString();

我试过但是没有插入记录,我的应用程序崩溃了。这是我的代码和日志猫堆栈跟踪。

这是我的活动代码。

public class Registration_Activity1 extends Activity
{
    TextView eText_TotalRecord;
    EditText editTextUserName,editTextEmailId , editTextPin,editTextConfirmPin;
    Button btnCreateAccount;
    Button btnViewDetails;

    DataBase_Adapter dbHandller;


    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registration_activity1);

         // get Instance  of Database Adapter
        dbHandller=new DataBase_Adapter(this);
        dbHandller=dbHandller.open();

        editTextUserName = (EditText) findViewById(R.id.editText_regName);
        editTextEmailId = (EditText) findViewById(R.id.editText_regEmail);
        editTextPin = (EditText) findViewById(R.id.editText_regApp_Pin);
        editTextConfirmPin = (EditText) findViewById(R.id.editText_regConfirm_App_Pin);



        btnCreateAccount=(Button)findViewById(R.id.reg_btnSubmit);
        btnCreateAccount.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                String userName=editTextUserName.getText().toString();
                String userEmail=editTextEmailId.getText().toString();
                String userPin=editTextPin.getText().toString();
                String userConfirmPin=editTextConfirmPin.getText().toString();


                // check if any of the fields are vaccant
                if(userName.equals("")||userEmail.equals("")||userPin.equals("")||userConfirmPin.equals(""))
                {
                        Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_SHORT).show();
                        return;
                }
                // check if both password matches
                if(!userPin.equals(userConfirmPin))
                {
                    Toast.makeText(getApplicationContext(), "Pin does not match", Toast.LENGTH_SHORT).show();
                    return;
                }
                else
                {
                    // Save the Data in Database
                    dbHandller.insertEntry(userName, userEmail, userPin);

                    Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


     @Override
        protected void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();

            dbHandller.close();
        }

}






11-26 18:21:30.635: E/AndroidRuntime(337): FATAL EXCEPTION: main
11-26 18:21:30.635: E/AndroidRuntime(337): java.lang.NullPointerException
11-26 18:21:30.635: E/AndroidRuntime(337):  at com.lead_management_project.Registration_Activity1$1.onClick(Registration_Activity1.java:50)
11-26 18:21:30.635: E/AndroidRuntime(337):  at android.view.View.performClick(View.java:2485)
11-26 18:21:30.635: E/AndroidRuntime(337):  at android.view.View$PerformClick.run(View.java:9080)
11-26 18:21:30.635: E/AndroidRuntime(337):  at android.os.Handler.handleCallback(Handler.java:587)
11-26 18:21:30.635: E/AndroidRuntime(337):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-26 18:21:30.635: E/AndroidRuntime(337):  at android.os.Looper.loop(Looper.java:123)
11-26 18:21:30.635: E/AndroidRuntime(337):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-26 18:21:30.635: E/AndroidRuntime(337):  at java.lang.reflect.Method.invokeNative(Native Method)
11-26 18:21:30.635: E/AndroidRuntime(337):  at java.lang.reflect.Method.invoke(Method.java:507)
11-26 18:21:30.635: E/AndroidRuntime(337):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-26 18:21:30.635: E/AndroidRuntime(337):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-26 18:21:30.635: E/AndroidRuntime(337):  at dalvik.system.NativeStart.main(Native Method)

这是我的注册xml

的xml文件
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="0"
    android:padding="5dp" >

    <!-- Table Row for Name -->

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:padding="5dp" >

        <EditText
            android:id="@+id/editText_regName"
            android:layout_width="290dp"
            android:layout_height="40dp"
            android:ems="10"
            android:hint="@string/regi_name"
            android:nextFocusDown="@+id/editText_regEmail"
            android:textSize="12sp" />
    </TableRow>

    <!-- Table Row for Email -->

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" >

        <EditText
            android:id="@+id/editText_regEmail"
            android:layout_width="290dp"
            android:layout_height="40dp"
            android:ems="10"
            android:hint="@string/regi_email"
            android:inputType="textEmailAddress"
            android:nextFocusDown="@+id/editText_regApp_Pin"
            android:textSize="12sp" />
    </TableRow>

    <!-- Table Row for Pn -->

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" >

        <EditText
            android:id="@+id/editText_regAppPin"
            android:layout_width="290dp"
            android:layout_height="40dp"
            android:ems="10"
            android:hint="@string/regi_pin"
            android:inputType="number"
            android:maxLength="4"
            android:nextFocusDown="@+id/editText_regConfirm_App_Pin"
            android:password="true"
            android:textSize="12sp" >

            <requestFocus />
        </EditText>
    </TableRow>

    <!-- Table Row for Confirm Pin -->

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" >

        <EditText
            android:id="@+id/editText_regConfirm_App_Pin"
            android:layout_width="290dp"
            android:layout_height="40dp"
            android:ems="10"
            android:hint="@string/regi_confirm_pin"
            android:inputType="number"
            android:maxLength="4"
            android:nextFocusDown="@+id/reg_btnSubmit"
            android:password="true"
            android:textSize="12sp" />
    </TableRow>

    <!-- Table Row for Button -->

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp" >

        <Button
            android:id="@+id/reg_btnSubmit"
            style="?android:attr/action"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_weight="1.25"
            android:text="@string/reg_btn_submit"
            android:textSize="12sp" />
    </TableRow>

</TableLayout>

0 个答案:

没有答案