-2

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

<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();

ADM
  • 20,406
  • 11
  • 52
  • 83
  • **TextInputLayout** not a text holder that you want, add id to **TextInputEditText** and take _text_ from it. – Sky May 12 '22 at 12:09
  • Checked out sources of **TextInputLayout** your aproach could work, try to check `individualName != null`. If it _null_ place view binding after setContentView() – Sky May 12 '22 at 12:15
  • Getting the same Error Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable com.google.android.material.textfield.TextInputEditText.getText()' on a null object reference – Vikas vikas May 12 '22 at 12:16
  • If null what should I do. – Vikas vikas May 12 '22 at 12:17
  • `super.onCreate(savedInstanceState);setContentView(R.layout.activity_add_individual);` these are your highest priorities, findViewById can't work if you haven't called `setContentView` yet – a_local_nobody May 12 '22 at 12:17
  • Can I know how to do setContentView – Vikas vikas May 12 '22 at 12:17
  • `setContentView(R.layout.activity_add_individual)` should be first line . You can not find view before setting the content view . SO move it up and super call also and it should work fine . – ADM May 12 '22 at 12:17

1 Answers1

1

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);
}
Ravi Mishra
  • 167
  • 1
  • 7