1

I have been getting this error since I implemented the

public void getOtherAnimals(){
            DatabaseReference otherAnimalDb = FirebaseDatabase.getInstance().getReference().child("Users").child(animalTwo);
            otherAnimalDb.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot snapshot, String s) {
                    if (snapshot.exists() && !snapshot.child("connections").child("no").hasChild(currentUId) && !snapshot.child("connections").child("yes").hasChild(currentUId)){
                        Cards item = new Cards(snapshot.getKey(), snapshot.child("name").getValue().toString());
                        rowItems.add(item);
                        arrayAdapter.notifyDataSetChanged();
                    }
                }

section of code, the program worked beautifully before. I have done my reading on NullPointerExceptions but I don't have enough experience working with them to find the issue. I understand my program is pointing to a null object, but I cannot find it.

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.adoptme, PID: 20525
java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
    at java.util.regex.Matcher.reset(Matcher.java:177)
    at java.util.regex.Matcher.<init>(Matcher.java:90)
    at java.util.regex.Pattern.matcher(Pattern.java:297)
    at com.google.firebase.database.core.utilities.Validation.isValidPathString(Validation.java:40)
    at com.google.firebase.database.core.utilities.Validation.validatePathString(Validation.java:44)
    at com.google.firebase.database.DataSnapshot.hasChild(DataSnapshot.java:80)
    at com.example.android.adoptme.MainActivity$5.onChildAdded(MainActivity.java:186)
    at com.google.firebase.database.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:79)
    at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Here is my code: (And yes I am aware that there is probably a better way to write it, I am still working on it and refining it with research).

package com.example.android.adoptme;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.FitWindowsViewGroup;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.lorentzos.flingswipe.SwipeFlingAdapterView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


        private Cards cards_data[];
        private arrayAdapter arrayAdapter;
        private int i;

        private FirebaseAuth firebaseAuth;

        private String currentUId;

        private DatabaseReference petDb;

        ListView listView;
        List<Cards> rowItems;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            petDb = FirebaseDatabase.getInstance().getReference().child("Users");

            firebaseAuth = FirebaseAuth.getInstance();

            checkAnimalType();

            rowItems = new ArrayList<Cards>();
            arrayAdapter = new arrayAdapter(this, R.layout.item, rowItems);

            SwipeFlingAdapterView flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);


            flingContainer.setAdapter(arrayAdapter);
            flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
                @Override
                public void removeFirstObjectInAdapter() {
                    // this is the simplest way to delete an object from the Adapter (/AdapterView)
                    Log.d("LIST", "removed object!");
                    rowItems.remove(0);
                    arrayAdapter.notifyDataSetChanged();
                }

                @Override
                public void onLeftCardExit(Object dataObject) {
                    Cards obj = (Cards) dataObject;
                    String userId = obj.getUserId();
                    petDb.child(userId).child(userId).child("connections").child("no").child(currentUId).setValue(true);

                    makeToast(MainActivity.this, "Left!");
                }

                @Override
                public void onRightCardExit(Object dataObject) {
                    Cards obj = (Cards) dataObject;
                    String userId = obj.getUserId();
                    petDb.child(userId).child(userId).child("connections").child("yes").child(currentUId).setValue(true);

                    makeToast(MainActivity.this, "Right!");


                }

                @Override
                public void onAdapterAboutToEmpty(int itemsInAdapter) {
                }

                @Override
                public void onScroll(float scrollProgressPercent) {

                }
            });


            // Optionally add an OnItemClickListener
            flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
                @Override
                public void onItemClicked(int itemPosition, Object dataObject) {
                    makeToast(MainActivity.this, "Clicked!");
                }
            });

        }

        static void makeToast(Context ctx, String s){
            Toast.makeText(ctx, s, Toast.LENGTH_SHORT).show();
        }

        private String animalOne, animalTwo;
        public void checkAnimalType(){

            final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

            DatabaseReference dogDb = FirebaseDatabase.getInstance().getReference().child("Users").child("Dog");
            dogDb.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded( DataSnapshot snapshot, String s) {
                    if (snapshot.getKey().equals(user.getUid())){
                        animalOne = "Dog";
                        animalTwo = "Cat";
                        getOtherAnimals();
                    }
                }

                @Override
                public void onChildChanged(DataSnapshot snapshot, String s) {
                }

                @Override
                public void onChildRemoved(DataSnapshot snapshot) {
                }

                @Override
                public void onChildMoved(DataSnapshot snapshot, String s) {
                }

                @Override
                public void onCancelled(DatabaseError error) {
                }
            });


            DatabaseReference catDb = FirebaseDatabase.getInstance().getReference().child("Users").child("Cat");
            catDb.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot snapshot, String s) {
                    if (snapshot.getKey().equals(user.getUid())){
                        animalOne = "Cat";
                        animalTwo = "Dog";
                        getOtherAnimals();
                    }
                }

                @Override
                public void onChildChanged(DataSnapshot snapshot, String s) {
                }

                @Override
                public void onChildRemoved( DataSnapshot snapshot) {
                }

                @Override
                public void onChildMoved(DataSnapshot snapshot, String s) {
                }

                @Override
                public void onCancelled(DatabaseError error) {
                }
            });
        }

        public void getOtherAnimals(){
            DatabaseReference otherAnimalDb = FirebaseDatabase.getInstance().getReference().child("Users").child(animalTwo);
            otherAnimalDb.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot snapshot, String s) {
                    if (snapshot.exists() && !snapshot.child("connections").child("no").hasChild(currentUId) && !snapshot.child("connections").child("yes").hasChild(currentUId)){
                        Cards item = new Cards(snapshot.getKey(), snapshot.child("name").getValue().toString());
                        rowItems.add(item);
                        arrayAdapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onChildChanged(DataSnapshot snapshot, String s) {
                }

                @Override
                public void onChildRemoved(DataSnapshot snapshot) {
                }

                @Override
                public void onChildMoved(DataSnapshot snapshot, String s) {
                }

                @Override
                public void onCancelled(DatabaseError error) {
                }
            });
        }

        public void logoutUser (View view){
            firebaseAuth.signOut();
            Intent intent = new Intent (MainActivity.this, LoginRegistration.class);
            startActivity(intent);
        }
    }

I appreciate any help you can provide me. Thanks in advance.

Deb
  • 59
  • 1
  • 7

1 Answers1

1

I think the .hasChild(currentUId) might be causing issues here. You should verify once if it's pointing to correct node in the Firebase DB or not. I couldn't really comment so had to post an answer only.

CapTen101
  • 476
  • 6
  • 12