-1

My project partner's code keeps crashing when I press a button to launch it in android studio. This is the error I get in the logcat.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference.

This refers to line 21 where: String fullName = name.getText().toString();

help?

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



    Intent intent = new Intent(MainActivity1.this, SecondActivity.class);

    EditText name = (EditText)findViewById(R.id.entName);
    String fullName = name.getText().toString();
    intent.putExtra("name_key", fullName);

    EditText ages = findViewById(R.id.age);
    String age = ages.getText().toString();
    intent.putExtra("age_key", age);

    RadioGroup gender = findViewById(R.id.genRGroup);
    int genderPos = gender.getCheckedRadioButtonId();
    int male = findViewById(R.id.male).getId();
    int female = findViewById(R.id.female).getId();
    String gen;

    if (genderPos == male) {
        gen = "Male";
    } else if (genderPos == female) {
        gen = "Female";
    } else {
        gen = "Other";
    }
    intent.putExtra("gen_key", gen);

    Button theBtn = findViewById(R.id.addAPersonButt);
    theBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intents = new Intent (MainActivity1.this, MainActivity2.class);
            startActivity(intents);
        }
    });

    Button conButt = findViewById(R.id.confirm);
    conButt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity1.this, SecondActivity.class);

            EditText name = findViewById(R.id.entName);
            String fullName = name.getText().toString();
            intent.putExtra("name_key", fullName);

            EditText ages = findViewById(R.id.age);
            String age = ages.getText().toString();
            intent.putExtra("age_key", age);

            RadioGroup gender = findViewById(R.id.genRGroup);
            int genderPos = gender.getCheckedRadioButtonId();
            int male = findViewById(R.id.male).getId();
            int female = findViewById(R.id.female).getId();
            String gen;

            if (genderPos == male) {
                gen = "Male";
            } else if (genderPos == female) {
                gen = "Female";
            } else {
                gen = "Other";
            }
            intent.putExtra("gen_key", gen);

            startActivity(intent);
        }
    });

}

}

  • 1
    Either the `name` is null (from the findViewById), or the `.getText()` is null; I'd bet on the former. Add a check prior to the chained call. – KevinO Nov 30 '18 at 00:18

1 Answers1

0

It crashes because of name.getText() is getting null values. Surround with if statement in String fullName = name.getText().toString();

Example :

String fullName;
if(!name.getText().toString().isEmpty()){
    fullName = name.getText().toString();
}
Sudip Sadhukhan
  • 1,784
  • 12
  • 21
  • This approach will not solve the issue. If `name` is null, this approach will still throw an NPE. – KevinO Nov 30 '18 at 14:33