尝试在我的数据库上存储数据时应用程序崩溃

时间:2016-01-22 10:09:18

标签: android

当我点击按钮将数据存储到数据库中时,我的应用程序提供以下NullPointerException

FATAL EXCEPTION: main Process: neoxsoft.com.taxmukti, PID: 28055 java.lang.NullPointerException
at neoxsoft.com.taxmukti.RegisterActivity$1.onClick(RegisterActivity.java:50)
at android.view.View.performClick(View.java:4452)
at android.view.View$PerformClick.run(View.java:18451)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5421)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:979)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
at dalvik.system.NativeStart.main(Native Method

这是我的代码:

public class RegisterActivity extends AppCompatActivity {
EditText fnm_et,lnm_et,eid_et,contact_et,pass_et,repass_et;
Button submit_btn,log_btn;
StoreData sd;
DataBaseService db=new DataBaseService(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    fnm_et= (EditText) findViewById(R.id.fnm_et);
    lnm_et= (EditText) findViewById(R.id.lnm_et);
    eid_et= (EditText) findViewById(R.id.eid_et);
    contact_et= (EditText) findViewById(R.id.contact_et);
    pass_et= (EditText) findViewById(R.id.pass_et);
    repass_et= (EditText) findViewById(R.id.repass_et);

    submit_btn= (Button) findViewById(R.id.submit_btn);
    log_btn= (Button) findViewById(R.id.log_btn);

    submit_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                String fname= fnm_et.getText().toString();
                String lname=lnm_et.getText().toString();
                String contact=contact_et.getText().toString();
                String mail=eid_et.getText().toString();
                String pass=pass_et.getText().toString();
                String repass=repass_et.getText().toString();
                Log.d("log1","getAlltext");

                sd.setF_name(fname);
                sd.setL_name(lname);
                sd.setMob_number(contact);
                sd.setE_mail(mail);
                sd.setUser_pass(pass);
                sd.setUser_repass(repass);
                Log.d("log2", "setAlltext");

                db.insertData(sd);
                Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
                RegisterActivity.this.finish();
        }
    });

    log_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
            startActivity(intent);
            RegisterActivity.this.finish();
        }
    });
}

}

  

我初始化了所有组件

fnm_et= (EditText) findViewById(R.id.fnm_et);
    lnm_et= (EditText) findViewById(R.id.lnm_et);
    eid_et= (EditText) findViewById(R.id.eid_et);
    contact_et= (EditText) findViewById(R.id.contact_et);
    pass_et= (EditText) findViewById(R.id.pass_et);
    repass_et= (EditText) findViewById(R.id.repass_et);

    submit_btn= (Button) findViewById(R.id.submit_btn);
    log_btn= (Button) findViewById(R.id.log_btn);

1 个答案:

答案 0 :(得分:0)

您的sd变量为null,并且正在抛出NPE。初始化它。