I am having a problem working my app with the new changes the tutorial demanded. I followed the coding rules exactly (with exception of the new 'snapshot' entries as per the new version) and yet my 'MainActivity' keeps crashing. Users are still being registered in the authentication but the database and isn't registering them as I would have liked.
The error console points towards 'DatabaseReference userDb = usersDb.child(user.getUid());' as the reason for crashing. I reckon there might be something wrong with the database, as it wasn't entirely clear how to set it up. Do let me know if there are new coding suggestions to get around this or whether I have missed something in the database setup. Please help.
Here is a link also to the tutorial I am following: https://www.youtube.com/watch?v=RagA8g9A5Qc&list=PLxabZQCAe5fio9dm1Vd0peIY6HLfo5MCf&index=17
private String userSex;
private String notUserSex;
public void checkUserSex() {
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference userDb = usersDb.child(user.getUid());
userDb.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
if (snapshot.child("sex").getValue() != null) {
userSex = snapshot.child("sex").getValue().toString();
switch (userSex) {
case "Male":
notUserSex = "Female";
break;
case "Female":
notUserSex = "Male";
break;
}
getOppositeSexUsers();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
public void getOppositeSexUsers(){
usersDb.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
if (snapshot.exists() && !snapshot.child("connections").child("no").hasChild(currentUId) && !snapshot.child("connections").child("yes").hasChild(currentUId) && snapshot.child("sex").getValue().toString().equals(notUserSex)){
String profileImageUrl = "default";
if(snapshot.child("profileImageUrl").getValue()!=null) {
if (!snapshot.child("profileImageUrl").getValue().equals("default")) {
profileImageUrl = snapshot.child("profileImageUrl").getValue().toString();
}
}
cards item = new cards(snapshot.getKey(), snapshot.child("name").getValue().toString(), profileImageUrl);
rowItems.add(item);
arrayAdapter.notifyDataSetChanged();
}
}
Logcat
2021-03-10 15:48:00.372 9915-9915/com.bendes.tinder2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bendes.tinder2, PID: 9915
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bendes.tinder2/com.bendes.tinder2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)' on a null object reference
at com.bendes.tinder2.MainActivity.checkUserSex(MainActivity.java:141)
at com.bendes.tinder2.MainActivity.onCreate(MainActivity.java:48)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2021-03-10 15:48:00.438 9915-9915/com.bendes.tinder2 I/Process: Sending signal. PID: 9915 SIG: 9
public class MainActivity extends AppCompatActivity {
private cards cards_data[];
private arrayAdapter arrayAdapter;
private int i;
private Button logout;
private FirebaseAuth mAuth;
private String currentUId;
private DatabaseReference usersDb;
ListView listView;
List<cards> rowItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkUserSex();
usersDb = FirebaseDatabase.getInstance().getReference().child("Users");
mAuth = FirebaseAuth.getInstance();
currentUId = mAuth.getCurrentUser().getUid();
logout = (Button) findViewById(R.id.signout);
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(MainActivity.this, ChooseLoginOrRegistrationActivity.class));
}
});