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
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);
}