Eclipse在提交表单时抛出NullPointerException

时间:2013-12-03 09:58:29

标签: android nullpointerexception

    **Edit :**

        package com.example.signup;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Dialog;
    import android.content.Intent;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class RegistrationPage extends Activity {
        EditText firstname, middlename, lastname, dob, country, username, mailid,password,confirmpassword;
        Button submit_registration_page;
        DBAdapter db;

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_registration_page);
            submit_registration_page= (Button) findViewById(R.id.submit_registration_button);
            submit_registration_page.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    if (firstname.getText().toString().length()==0) {
                        firstname.setError("Enter your firstname");
                    } else if (middlename.getText().toString().length() == 0) {
                        middlename.setError("Enter your middlename");
                    } else if (lastname.getText().toString().length() == 0) {
                        lastname.setError("Enter your lastname");
                    } else if (dob.getText().toString().length() == 0) {
                        dob.setError("Enter your date of birth");
                    } else if (country.getText().toString().length() == 0) {
                        country.setError("Enter your habitat");
                    } else if (username.getText().toString().length() == 0) {
                        username.setError("Please enter your username");
                    } else if (mailid.getText().toString().length() == 0) {
                        mailid.setError("Please enter your e-mail id");
                    } else if (password.getText().toString().length() == 0) {
                        password.setError("Please enter your password");
                    } else if (confirmpassword.getText().toString().length() == 0) {
                        confirmpassword.setError("Please re-enter your password");
                    }
                    if(!password.equals(confirmpassword))
                    {
                        Dialog d = new Dialog(RegistrationPage.this);
                        d.setTitle("Passwords do not match");
                        TextView tv = new TextView(RegistrationPage.this);
                        tv.setText("Please re-enter the passwords");
                        d.setContentView(tv);

                    }

                    db.insertRecord(firstname.getText().toString(),middlename.getText().toString(),lastname.getText().toString(),dob.getText().toString(),country.getText().toString(),username.getText().toString(),mailid.getText().toString(),password.getText().toString(),confirmpassword.getText().toString());
                    Log.i("RegistrationPage","Records are inserted");
                    Intent profile_intent = new Intent(RegistrationPage.this,Profile_view.class);
                    startActivity(profile_intent);

                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.registration_page, menu);
            return true;
        }

    }

**Edit StackTrace :**

     12-03 16:33:14.895: E/AndroidRuntime(707): FATAL EXCEPTION: main
    12-03 16:33:14.895: E/AndroidRuntime(707): java.lang.NullPointerException
    12-03 16:33:14.895: E/AndroidRuntime(707):  at com.example.signup.RegistrationPage$1.onClick(RegistrationPage.java:26)
    12-03 16:33:14.895: E/AndroidRuntime(707):  at android.view.View.performClick(View.java:2485)
    12-03 16:33:14.895: E/AndroidRuntime(707):  at android.view.View$PerformClick.run(View.java:9080)
    12-03 16:33:14.895: E/AndroidRuntime(707):  at android.os.Handler.handleCallback(Handler.java:587)
    12-03 16:33:14.895: E/AndroidRuntime(707):  at android.os.Handler.dispatchMessage(Handler.java:92)
    12-03 16:33:14.895: E/AndroidRuntime(707):  at android.os.Looper.loop(Looper.java:123)
    12-03 16:33:14.895: E/AndroidRuntime(707):  at android.app.ActivityThread.main(ActivityThread.java:3683)
    12-03 16:33:14.895: E/AndroidRuntime(707):  at java.lang.reflect.Method.invokeNative(Native Method)
    12-03 16:33:14.895: E/AndroidRuntime(707):  at java.lang.reflect.Method.invoke(Method.java:507)
    12-03 16:33:14.895: E/AndroidRuntime(707):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    12-03 16:33:14.895: E/AndroidRuntime(707):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    12-03 16:33:14.895: E/AndroidRuntime(707):  at dalvik.system.NativeStart.main(Native Method)
    12-03 16:33:18.405: I/Process(707): Sending signal. PID: 707 SIG: 9

当我提供所有细节并提交时,我得到了异常,并且还要注意我只是将数据插入数据库,然后调用intent.insert(...)方法在另一个类中实现。

     The logcat says null pointer exception in line 26 which is the line where i verify the if condition for firstname.getText().toString().length()==0 .

  Help me as I am new to programming and android.

2 个答案:

答案 0 :(得分:0)

不是答案:使用

firstname.isEmpty()而不是

firstname.getText().toString().length() == 0

代码需要很长时间才能验证。然后更新第37行

我的猜测是:db未打开,因此null

答案 1 :(得分:0)

EditText之后onCreate()初始化setContentView()个变量:

firstname = (EditText)findViewById(R.id.id_of_firstname_edittext);
middlename = ...