j doe
j doe

Reputation: 73

Firebase - (DataSnapshot with multiple child nodes) - NullPointerException

Goal: I am trying to pull data from Firebase and insert the values into Strings from nested nodes (in Android.)

Note: It's FINE for the first variable/key-value pair(mNum) but gets NullPointer for the others


Research: Firebase docs, Youtube, Stackoverflow about this specific question including: How to get child of child value from firebase in android?

How this applies to others: Trouble finding information on getting Firebase database information when DB has multiple nestings


Error:

java.lang.NullPointerException: println needs a message
                                                                                at android.util.Log.println_native(Native Method)
                                                                                at android.util.Log.i(Log.java:211)
                                                                                at com.netgalaxystudios.timeclock.RegisterSubscriptionActivity$1.onDataChange(RegisterSubscriptionActivity.java:80)

The error points to this line: Log.i("micro name", micro);


Database: Picture of Firebase DB that is being used enter image description here


RegisterSubscriptionActivity.java:

public class RegisterSubscriptionActivity extends Activity {

    private DatabaseReference mDatabase;
    private DatabaseReference mDatabaseMicro;

    ArrayList subscriptionInfo;

    //Subscription (String) values
    String micro, small, medium, large, enterprise;
    String mNum, sNum, medNum, lNum, eNum;
    String mPrice, sPrice, medPrice, lPrice, ePrice;


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

        subscriptionInfo = new ArrayList<>();


        //GET INSTANCE OF FIREBASE DB & GRAB SUBSCRIPTION DATA
        mDatabase = FirebaseDatabase.getInstance().getReference("Subscription");
        mDatabaseMicro = mDatabase.child("Micro");
        mDatabaseMicro.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) { //literally a "snapshot" of the data
                //dataSnapshot.

                for(DataSnapshot subscriptionDataSnapshot : dataSnapshot.getChildren()) {

                    if (subscriptionDataSnapshot.getKey().equals("mNum")) {
                        mNum= subscriptionDataSnapshot.getValue().toString();
                    }
                    if(subscriptionDataSnapshot.getKey().equals("mPrice")) {
                        mPrice = subscriptionDataSnapshot.getValue().toString();
                    }
                    if(subscriptionDataSnapshot.getKey().equals("micro")) {
                        micro = subscriptionDataSnapshot.getValue().toString();
                    }

                    Log.i("micro number", mNum);
                    //Log.i("micro price", mPrice);
                    Log.i("micro name", micro);

                    String subscriptions = subscriptionDataSnapshot.getValue(String.class); //Each time it gets the key/value pair per node
                    subscriptionInfo.add(subscriptions);


                }

Upvotes: 1

Views: 2528

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138824

To get the data under Micro node, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference microRef = rootRef.child("Subscription").child("Micro");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String mNum = ds.child("mNum").getValue(String.class);
            String mPrice = ds.child("mPrice").getValue(String.class);
            String micro = ds.child("micro").getValue(String.class);
            Log.d("TAG", mNum + " / " + mPrice + " / " + micro);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
microRef.addListenerForSingleValueEvent(eventListener);

Your outout will be:

10 Employees / 25 / Micro

Upvotes: 1

John O&#39;Reilly
John O&#39;Reilly

Reputation: 10330

Did you mean to put those log statements outside the for loop? The first time through loop it will match on "mNum" but micro will still be null.

Upvotes: 0

Related Questions