0

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" : "1@gmai.com",
        "username" : "1234"
      }
    },
    "Driver" : {
      "S04QBZx3PxYaLfDWBC3j3otM5ml1" : {
        "mail" : "m.yusuf7423@gmail.com",
        "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" />
  • 1
    The first step is to stop ignoring potential errors and implementing `onCancelled`. At its minimum, this should look like this: `public void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); }` – Frank van Puffelen Jun 25 '21 at 22:43
  • heyy i tried that but i am not able to call it. I have edited the code for the reference – Mohammad Yusuf Jun 26 '21 at 07:13

1 Answers1

1

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;
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I ran the code after the edits but now it is always returning false which means that it is always null .. How do I solve that issue? – Mohammad Yusuf Jun 26 '21 at 15:51
  • Yeah, I now realized that your condition is inverted. I updated the code in my answer, but I also highly recommend stepping through this type of problem in a debugger. If you set a breakpoint on the `if (mail == null || !mail.equals(email.getText().toString())) {` and check the values, the code typically makes a lot more sense - and you'll often spot problems much faster than by staring at the same code in the editor (or on Stack Overflow). – Frank van Puffelen Jun 26 '21 at 16:25
  • yea I checked that and i saw my name is coming as null but I am storing the username value innit and it still staying null – Mohammad Yusuf Jun 26 '21 at 17:45
  • OK, that's a different problem then - and most likely it's because your `update ` method gets executed before the `onDataChange` ever runs. I recommend checking these questions for more on this asynchronous nature: https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 https://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener – Frank van Puffelen Jun 26 '21 at 18:08