JNR_Prog123 JNR_Prog123 - 1 month ago 6
Android Question

App crashes when writing to SQLite Database in Android

I have developed an app, however the User Registration seems to crash the app. I have checked everything but cannot find the cause, if anyone can assist. My code is below.

java class

public class SignUp extends AppCompatActivity {

DatabaseHelper helper = new DatabaseHelper(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);

}

public void onSignupClick (View view)
{
if (view.getId() == R.id.BsignUp)
{
EditText name = (EditText)findViewById(R.id.TFusername);
EditText email = (EditText)findViewById(R.id.TFemail);
EditText uname = (EditText)findViewById(R.id.TFuname);
EditText pass1 = (EditText)findViewById(R.id.TFpass1);
//EditText pass2 = (EditText)findViewById(R.id.TFpass2);

String namestr = name.getText().toString();
String emailstr = email.getText().toString();
String unamestr = uname.getText().toString();
String pass1str = pass1.getText().toString();

Contact c = new Contact();
c.setName(namestr);
c.setEmail(emailstr);
c.setUname(unamestr);
c.setPass(pass1str);

helper.insertContact(c);

}
}
}


xml layout

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView
android:layout_width="match_parent"
android:src ="@drawable/saica"
android:layout_height="120dp" />

<TextView
android:text="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/TFname" />

<TextView
android:text="Email Address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView3" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/TFemail" />

<TextView
android:text="Username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView4" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/TFuname" />

<TextView
android:text="Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView5" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/TFpass1"
android:password="true" />

<Button
android:text="Register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/BsignUp"
android:onClick="onSignupClick"/>


</LinearLayout>


Error Log

FATAL EXCEPTION: main
Process: com.example.pooveshin.saica_sgb, PID: 10321
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.pooveshin.saica_sgb.SignUp.onSignupClick(SignUp.java:30)
at java.lang.reflect.Method.invoke(Native Method)

Answer

You don't have any EditText named TFusername, your actual EditText id is TFname. So it should be

EditText name = (EditText)findViewById(R.id.TFname);