Reputation: 77
So basically I created a page where the user can edit his name and email but it doesn't work when and the data remains the same in firebase. I tried calling the update function inside the on-create but that doesn't seem to work I even tried writing the whole code directly inside the on-create but the app crashed so I had decided to create the function update() and I tried calling the the update() function inside on-click but even that didnt work
this is my code
public class Profile extends AppCompatActivity {
private FirebaseUser user;
private DatabaseReference reference;
private String userID;
EditText Username,email;
String name,mail;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
Username=findViewById(R.id.Username1);
email=findViewById(R.id.emailadress);
button=findViewById(R.id.editinfo);
user= FirebaseAuth.getInstance().getCurrentUser();
reference= FirebaseDatabase.getInstance().getReference("Users");
userID=user.getUid();
reference.child("Driver").child(userID).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
Users usersnapshot=snapshot.getValue(Users.class);
if(usersnapshot!=null){
String username=usersnapshot.getUsername();
String Email=usersnapshot.getMail();
String password=usersnapshot.getPassword();
Username.setText(username);
email.setText(Email);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
button.setOnClickListener(new android.view.View.OnClickListener() {
@Override
public void onClick(android.view.View v) {
}
});
}
public void update(){
if(isNamechanged()||isEmailchanged()){
Toast.makeText(this,"Data has been updated",Toast.LENGTH_LONG).show();
}
}
public void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); }
private boolean isEmailchanged() {
if (!mail.equals(email.getText().toString())) {
reference.child("Driver").child(userID).child("mail").setValue(email.getText().toString());
mail=email.getText().toString();
return true;
} else {
return false;
}
}
private boolean isNamechanged() {
if (!name.equals(Username.getText().toString())) {
reference.child("Driver").child(userID).child("username").setValue(Username.getText().toString());
name=Username.getText().toString();
return true;
}
else {
return false;
}
}
}
This is my database
{
"Users" : {
"Customer" : {
"0awTFPGUkIdzAaZEgYZMmGv1zwk1" : {
"mail" : "[email protected]",
"username" : "1234"
}
},
"Driver" : {
"S04QBZx3PxYaLfDWBC3j3otM5ml1" : {
"mail" : "[email protected]",
"username" : "YUSUF"
}
}
}
}
Also now my app started to crash and I get this problem in logcat
Process: com.example.deliveryapp, PID: 12806
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
Edit:in the layout page I added On-click option to call update so now it doesn't crash and doesn't throw any error but now nothing happens at all like I can here the sound of the button being clicked(which happens when there is an onclick function) but nothing happens at all this is the layout of that button
<Button
android:id="@+id/editinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="228dp"
android:layout_marginLeft="228dp"
android:layout_marginTop="456dp"
android:onClick="update"
android:text="edit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Upvotes: 0
Views: 829
Reputation: 598728
Given the NullPointerException
, it seems that isEmailchanged
or isNamechanged
is failing, because mail
or name
is null.
That makes sense, as you don't initialize the name
and email
fields anywhere, so the first time your isEmailchanged
or isNamechanged
is called, they are likely to still null and you end up calling null.equals(...)
which raises the exception.
It's up to you to determine what you want your application to do in this case, but a simple fix would be to check for null:
private boolean isEmailchanged() {
if (mail == null || !mail.equals(email.getText().toString())) {
reference.child("Driver").child(userID).child("mail").setValue(email.getText().toString());
mail=email.getText().toString();
return true;
} else {
return false;
}
}
private boolean isNamechanged() {
if (name == null || !name.equals(Username.getText().toString())) {
reference.child("Driver").child(userID).child("username").setValue(Username.getText().toString());
name=Username.getText().toString();
return true;
}
else {
return false;
}
}
Upvotes: 1