I am trying to use Shared Preferences and send my data into another activity. When I click on the registration textview the application crashes and I get the error message
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.EditText com.google.android.material.textfield.TextInputLayout.getEditText()' on a null object reference
at com.example.irongym.RegistrationActivity.onCreate(RegistrationActivity.java:45)
In other cases where I was using EditText, I used a similar way and it was working. I think that the way I try to get the data is wrong because now I am using TextInputLayout
This is my RegistrationActivity.java (onCreate)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences file = getSharedPreferences("userData", MODE_PRIVATE);
TextInputLayout nameField = findViewById(R.id.TextInputEditText);
TextInputLayout passwordField = findViewById(R.id.PasswordInputEditText);
TextInputLayout phoneField = findViewById(R.id.PhoneInputEditText);
String na = file.getString("name", "");
String pas = file.getString("password2", "");
String ph = file.getString("phone", "");
nameField.getEditText().setText(na); // This is line 45 from the error i get
passwordField.getEditText().setText(pas);
phoneField.getEditText().setText(ph);
This is my RegistrationActivity.java button onclick
public void confirmInput(View v) {
SharedPreferences file = getSharedPreferences("userData", MODE_PRIVATE);
SharedPreferences.Editor editor = file.edit();
TextInputLayout nameField = findViewById(R.id.TextInputEditText);
TextInputLayout passwordField = findViewById(R.id.PasswordInputEditText);
TextInputLayout phoneField = findViewById(R.id.PhoneInputEditText);
String name = nameField.getEditText().getText().toString();
String password2 = passwordField.getEditText().getText().toString();
String phone = phoneField.getEditText().getText().toString();
editor.putString("name", name);
editor.putString("password2", password2);
editor.putString("phone", phone);
editor.commit();
Intent in = new Intent(this, LoginActivity.class);
startActivity(in);
}
This is my ProfileActivity.java where the data will be displayed
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityProfileBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
SharedPreferences file = getSharedPreferences("userData", MODE_PRIVATE);
String we = file.getString("weight", "no-weight");
String he = file.getString("height", "no-height");
String na = file.getString("name", "no-name");
String ph = file.getString("phone", "no-phone");
String ps = file.getString("password2", "no-password");
TextView tvWe = findViewById(R.id.tvWeight);
TextView tvHe = findViewById(R.id.tvHeight);
TextView tvNa = findViewById(R.id.tvName);
TextView tvPh = findViewById(R.id.tvPhone);
TextView tvPs = findViewById(R.id.tvPassword);
tvWe.setText(we);
tvHe.setText(he);
tvNa.setText(na);
tvPh.setText(ph);
tvPs.setText(ps);
binding.fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
CodePudding user response:
You forgot to call setContentView in RegistrationActivity
CodePudding user response:
Specifically, because you haven't called setContentView
there are no View
s in your Activity. So
TextInputLayout nameField = findViewById(R.id.TextInputEditText);
returns null because that View
wasn't found. Because nameField
is null, you can't call this method on it:
nameField.getEditText() // crashes with a NullPointerException
And that's basically what the error is telling you:
Caused by: java.lang.NullPointerException:
Attempt to invoke virtual method
'android.widget.EditText com.google.android.material.textfield.TextInputLayout.getEditText()'
on a null object reference
You tried to call the getEditText()
method on something that's meant to be a TextInputLayout
, but it's actually null. You just need to find out why!