Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why am I getting error of 'android.widget.EditText com.google.android.material.textfield.TextInputLayout.getEditText()' on a null object reference

I have seen many forums related to this question but nothing resolved my issue. I hope I get it here.

Here is my Activity Code

public class addIndividual extends AppCompatActivity {
public TextInputLayout individualName;
public TextInputLayout individualPhno;
FirebaseFirestore db;
String name;
String phno;

@Override
protected void onCreate(Bundle savedInstanceState) {
    db=FirebaseFirestore.getInstance();
    individualName  = (TextInputLayout) findViewById(R.id.individualName);
    individualPhno= (TextInputLayout) findViewById(R.id.individualPhno);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_individual);

}


public void submitButton(View view) {
    name= individualName.getEditText().getText().toString();
    phno= "hello";
    Map<String,String> individualInfo = new HashMap<String,String >();
    individualInfo.put("name",name.trim());
    individualInfo.put("phNo",phno.trim());

    db.collection("individuals").add(individualInfo)
            .addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                @Override
                public void onComplete(@NonNull Task<DocumentReference> task) {

                    Toast.makeText(addIndividual.this, "Inserted Successfully", Toast.LENGTH_SHORT).show();
                }
            });


}
}

XML Code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_margin="10dp"
tools:context=".PSLogin.addIndividual">

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



    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/individualName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Individual Name"
        android:layout_below="@id/textView1"
        android:layout_marginTop="40dp"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        >

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/individualPhno"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Individual Phone Number"
        android:layout_below="@id/username"
        android:layout_marginTop="30dp"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        >

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            />

    </com.google.android.material.textfield.TextInputLayout>

    <Button
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:layout_below="@id/password"
        android:onClick="submitButton"
        android:layout_marginTop="30dp"
        android:gravity="center"
        style="?attr/materialButtonOutlinedStyle"
        />

</LinearLayout>

</LinearLayout>
enter code here

I have changed the TextInputLayout to EditText but the problem persists no result
Error
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘android.text.Editable android.widget.EditText.getText()’ on a null object reference
When
public EditText individualName;

Error is occurring at name= individualName.getEditText().getText().toString();

>Solution :

In your onCreate(), you are initialising you view views before setting view. Your onCreate should look like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_individual);
    db=FirebaseFirestore.getInstance();
    individualName  = (TextInputLayout)findViewById(R.id.individualName);
individualPhno= (TextInputLayout) findViewById(R.id.individualPhno);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading