Android Eclipse在提交表单时抛出NullPointerException

Android Eclipse在提交表单时抛出NullPointerException,android,nullpointerexception,Android,Nullpointerexception,当我给出所有细节并提交时,我得到了一个异常,而且需要注意的是,我只是将数据插入数据库,然后调用intent.insert(…)方法在另一个类中实现 **Edit :** package com.example.signup; import android.os.Bundle; import android.app.Activity; import android.app.Dialog; import android.content.In

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

    **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
答案不是:使用

firstname.isEmpty()
而不是

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

代码太长,无法验证。然后更新第45行


我的猜测是:
db
未打开,因此
null
setContentView()之后在
onCreate()
中初始化
EditText
变量:


添加完整的代码或让我们知道代码的哪一部分是第45位line@San在您的一个细节中传递空值。请输入
第45行的代码。谢谢您指出。我从没想过我会犯这样愚蠢的错误。它的工作原理是,它没有抛出相同的错误,但在尝试启动活动时表示classcastexception。这方面有什么想法吗。感谢您的努力。@San首先检查布局XML元素是否与您试图将
findViewById(R.id.something)
的结果强制转换为的类型相同。此外,如果您最近编辑了资源,则资源ID可能不同步。在这种情况下,清理和重建您的项目。有谁能帮我这个原因,我已经坐在它很长一段时间了。
     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.
firstname = (EditText)findViewById(R.id.id_of_firstname_edittext);
middlename = ...