I am trying to fix this null pointer exception issue from the past 24 hours. It is constantly showing error in getOppositeSexUsers()
. I have asked questions and also got the reply but it showing the Null Pointer Exception evey time.
This is the logcat .
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.techjd.hubu.MainActivity$6.onChildAdded(MainActivity.java:205)
at com.google.firebase.database.core.ChildEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.0:79)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.0:55)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5740)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766)
This is the main activity code .
public void getOppositeSexUsers(){
DatabaseReference oppositeSexDb = FirebaseDatabase.getInstance().getReference().child("Users").child(oppositeUserSex);
oppositeSexDb.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.exists() && !dataSnapshot.child("connections").child("nope").hasChild(currentUId) && !dataSnapshot.child("connections").child("yes").hasChild(currentUId)){
Object ImageData = dataSnapshot.child("profileImageUrl").getValue();
String ImageDefault = "default";
if (ImageData != null){
ImageDefault = ImageData.toString();
}
cards item = new cards(dataSnapshot.getKey(), dataSnapshot.child("Name").getValue().toString() , ImageDefault);
//cards item = new cards(dataSnapshot.getKey(), dataSnapshot.child("Name").getValue().toString(), dataSnapshot.child("profileImageUrl").getValue().toString());
rowItems.add(item);
arrayAdapter.notifyDataSetChanged();
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Everytime I try a solution in getOppositeSexUsers() It will generate Null Pointer Exception . What is fix ?