I try to display customer profile on my apps, but these line of code makes app crash when I click on the profile button(profile page)
the error said that Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
my name and address can be displayed but not my email and my phone number which is weird why it happen like that?
I check my database it contains data, I got stuck here on how to solve my problems because the code can't read my email and phone number but can ready my name and address.
public class CustProfileActivity extends AppCompatActivity {
TextView email;
EditText name, address, phone;
Button update;
DatabaseReference databaseReference;
FirebaseAuth firebaseAuth;
String userId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cust_profile2);
name = findViewById(R.id.name);
address = findViewById(R.id.address);
update = findViewById(R.id.update);
databaseReference = FirebaseDatabase.getInstance().getReference("Customer").child("");
firebaseAuth = FirebaseAuth.getInstance();
userId = firebaseAuth.getUid();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Customer cust = dataSnapshot.child(userId).getValue(Customer.class);
name.setText(cust.name);
address.setText(cust.home_address);
phone.setText(cust.telephone_number);
email.setText(cust.email);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Phone No"
android:textSize="13sp"/>
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="13dp"/>
<TextView
android:id="@+id/email"
android:hint="email@gmail.com"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
public class Customer {
public String id;
public String name;
public String email;
public String home_address;
public String telephone_number;
public Customer(){
}
public Customer(String id, String name, String email, String home_address, String telephone_number) {
this.id = id;
this.name = name;
this.email = email;
this.home_address = home_address;
this.telephone_number = telephone_number;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getHome_address() {
return home_address;
}
public void setHome_address(String home_address) {
this.home_address = home_address;
}
public String getTelephone_number() {
return telephone_number;
}
public void setTelephone_number(String telephone_number) {
this.telephone_number = telephone_number;
}