0

So really new to Java and was wondering why I keep getting this error: Method invocation 'equals' and 'getUId may produce java NullpointerException. Every time I launch the app on my device and hit the sign out button it crashes. The tutorial I am watching it doesn't happen, so I'm wondering what I'm doing wrong. Any input would be appreciated.

DatabaseReference femaleDb = FirebaseDatabase.getInstance().getReference().child("Users").child("Female");
femaleDb.addChildEventListener(new ChildEventListener() {
   @Override
   public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
      if(dataSnapshot.getKey().equals(user.getUid())){
         userSex = "Female";
         oppositeUserSex = "Male";
         getOppositeSexUsers();
      }
   }
   @Override
   public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
   }
   @Override
   public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
   }
   @Override
   public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
   }
   @Override
   public void onCancelled(@NonNull DatabaseError databaseError) {

   }
});
}

public void getOppositeSexUsers(){
   DatabaseReference oppositeSexDb = FirebaseDatabase.getInstance().getReference().child("Users").child(oppositeUserSex);
   oppositeSexDb.addChildEventListener(new ChildEventListener() {
      @Override
      public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
         if(dataSnapshot.exists() && !dataSnapshot.child("connections").child("nope").hasChild(currentUId) && !dataSnapshot.child("connections").child("yes").hasChild(currentUId)){

            cards item = new cards(dataSnapshot.getKey(), dataSnapshot.child("name").getValue().toString());
            rowItems.add(item);
            arrayAdapter.notifyDataSetChanged();
         }
      }
      @Override
      public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
      }
      @Override
      public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
      }
      @Override
      public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
      }
      @Override
      public void onCancelled(@NonNull DatabaseError databaseError) {

      }
   });

Error:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.e.tinderclone, PID: 4030 java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference

Yoshikage Kira
  • 1,070
  • 1
  • 12
  • 20
  • 1
    Java normally provides a pretty informative stack trace. That should tell you the line of your code that is referencing a null object. – Joseph Larson Sep 13 '19 at 15:55
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Yoshikage Kira Sep 13 '19 at 15:57
  • @JosephLarson yeah it does, the only issue is that I don't know how to go about fixing it... –  Sep 13 '19 at 16:21
  • It points to this line: if(dataSnapshot.exists() && !dataSnapshot.child("connections").child("nope").hasChild(currentUId) && !dataSnapshot.child("connections").child("yes").hasChild(currentUId)){ –  Sep 13 '19 at 16:25
  • Okay, then test if dataSnapshot is null. Test if the various dataSnapshot.child methods return null. Maybe that method should do a print statement if it's about to return null. – Joseph Larson Sep 17 '19 at 20:28

1 Answers1

1

As you can see in the docs, getKey can become null, causing your app to crash because equals cannot be called on null.

You could simply introduce variables for objects that could become null (see the warnings), and ensure they are not null before using them. Here is a basic example:

public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
    String key = dataSnapshot.getKey(); // introduce variable
    if (key != null && key.equals(user.getUid())){ // check for null before usage
        userSex = "Female";
        oppositeUserSex = "Male";
        getOppositeSexUsers();
    }
}
user7217806
  • 2,014
  • 2
  • 10
  • 12
  • Okay, so I changed the code to reflect what you said above, but what should I do about line 186 which is the one throwing the warning now? if(dataSnapshot.exists() && !dataSnapshot.child("connections").child("nope").hasChild(currentUId) && !dataSnapshot.child("connections").child("yes").hasChild(currentUId)){ –  Sep 15 '19 at 11:47
  • could you shed some light on that? Thanks! –  Sep 15 '19 at 12:21
  • Could you please post the warning message? – user7217806 Sep 15 '19 at 15:09
  • This is the error that I am getting on line 190. E/AndroidRuntime: FATAL EXCEPTION: main Process: com.e.tinderclone, PID: 6957 java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference at com.e.tinderclone.MainActivity$6.onChildAdded(MainActivity.java:190) –  Sep 15 '19 at 18:49
  • if(dataSnapshot.exists() && !dataSnapshot.child("connections").child("nope").hasChild(currentUId) && !dataSnapshot.child("connections").child("yeps").hasChild(currentUId)){ cards items= new cards(dataSnapshot.getKey(), Objects.requireNonNull(dataSnapshot.child("name").getValue()).toString()); –  Sep 15 '19 at 18:53
  • that's the code and the logcat error... I don't know how to make it look neater –  Sep 15 '19 at 18:55
  • There again is a call to `dataSnapshot.getKey()` for which you should apply the same technique as before. Also, to narrow down the source of the error, try commenting the code inside the if, see if you still get the error, try commenting the if itself, see if you get the error, and so on. – user7217806 Sep 15 '19 at 19:33