Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
方法调用equals可能会产生java NullpointerException_Java_Methods_Nullpointerexception_Equals - Fatal编程技术网

方法调用equals可能会产生java NullpointerException

方法调用equals可能会产生java NullpointerException,java,methods,nullpointerexception,equals,Java,Methods,Nullpointerexception,Equals,所以对Java来说真的很陌生,我想知道为什么我总是会遇到这个错误:方法调用'equals'和'getUId可能会产生Java NullpointerException。每次我在设备上启动应用程序并按下注销按钮时,它都会崩溃。我正在看的教程没有发生,所以我想知道我做错了什么。如有任何意见,将不胜感激 DatabaseReference femaleDb = FirebaseDatabase.getInstance().getReference().child("Users").child("Fem

所以对Java来说真的很陌生,我想知道为什么我总是会遇到这个错误:方法调用'equals'和'getUId可能会产生Java NullpointerException。每次我在设备上启动应用程序并按下注销按钮时,它都会崩溃。我正在看的教程没有发生,所以我想知道我做错了什么。如有任何意见,将不胜感激

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) {

      }
   });
错误:

E/AndroidRuntime:致命异常:主 进程:com.e.tinderclone,PID:4030 java.lang.NullPointerException:尝试对空对象引用调用接口方法“int java.lang.CharSequence.length”

正如您在中所看到的,getKey可能会变为null,导致应用程序崩溃,因为不能在null上调用equals

您可以简单地为可能变为null的对象引入变量。请参阅警告,并在使用它们之前确保它们不为null。以下是一个基本示例:

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();
    }
}

Java通常提供一个非常有用的堆栈跟踪。这应该告诉你代码中引用空对象的那一行。可能是@JosephLarson的重复是的,唯一的问题是我不知道如何修复它…它指向这一行:ifdataSnapshot.exists&!dataSnapshot.childconnections.childnope.hasChildcurrentUId&!dataSnapshot.childconnections.childyes.hasChildcurrentUId{好的,然后测试dataSnapshot是否为null。测试各种dataSnapshot.child方法是否返回null。如果该方法即将返回null,可能该方法应该执行一个print语句。好的,所以我更改了代码以反映您上面所说的内容,但是对于现在抛出警告的第186行,我应该怎么做?ifdataSnapshot.exists&!dataSnapshot.childconnections.childnope.hasChildcurrentUId&!dataSnapshot.childconnections.childyes.hasChildcurrentUId{你能解释一下吗?谢谢!你能发布警告消息吗?这是我在第190行遇到的错误。E/AndroidRuntime:FATAL EXCEPTION:main Process:com.E.tinderclone,PID:6957 java.lang.NullPointerException:尝试在com上的空对象引用上调用接口方法'int java.lang.CharSequence.length'.e.tinderclone.MainActivity$6.onChildAddedMainActivity.java:190ifdataSnapshot.exists&&!dataSnapshot.childconnections.childnope.hasChildcurrentUId&&!dataSnapshot.childconnections.childyeps.hasChildcurrentUId{cards items=new cardsdataSnapshot.getKey,Objects.RequiredOnNullDataSnapshot.childname.getValue.toString;