0

I've fetched data from Firebase to the Dashboard (Fragment) Main class , and now I need to access that same ArrayList to another Fragment class. How do i share arraylists between two classes.

My Code from the Dashboard Class:

private void GetCategory() {


    DatabaseReference db = FirebaseDatabase.getInstance().getReference("Category");
    db.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
                Catergory_List cl = userSnapshot.getValue(Catergory_List.class);
                CatList.add(cl);

            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });
}

My Second Class

private Dashboard db;
            TExpenses= db.TrasactionExpenses();
        System.out.println(TExpenses.size());

And i tried this code and its not working

  public List<Catergory_List> TrasactionExpenses() {
        return CatList;
    }

Error Log

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List Dashboard.TrasactionExpenses()' on a null object reference

Any help would be appreciated..i'm unable to get the Values from Dashboard Class ArrayList to Class B

2 Answers2

1

It's normal, you did not initialize your Dashboard !

Yanis02015
  • 65
  • 6
1

I'm not sure what the constructor for Dashboard looks like, but you'll need to initialize the Dashboard object. You've only declared declared the dashboard db. (See the first answer for initialization vs. instantiation vs. declaration Difference between initializing a class and instantiating an object?)

You'll need to do something like:

Dashboard db = new Dashboard();

in your second class.

mfcarroll
  • 11
  • 2