1

enter image description hereI am trying to read data that I inserted into firebase database, I want to display the data in custom listView. I am using this method but it seems that I am missing something. I can't insert data into my arraylist. UPDATED I inserted the values and now it gives me

attempt to invoke a virtual method on a null object reference on my get methods

private void showData(DataSnapshot dataSnapshot) {
    for (DataSnapshot ds : dataSnapshot.getChildren()) {
        PatientInformation patientInfo = new PatientInformation();
        patientInfo.setName(ds.child(userID).getValue(PatientInformation.class).getName());
        patientInfo.setAge(ds.child(userID).getValue(PatientInformation.class).getAge());
        patientInfo.setDate(ds.child(userID).getValue(PatientInformation.class).getDate());
        ArrayList<PatientInformation> arrayList = new ArrayList<>();
        arrayList.add(patientInfo);
        PatientInformationAdapter pAdapter = new PatientInformationAdapter(this, arrayList);
        listView.setAdapter(pAdapter);
    }
1
  • We're missing some information here that is needed to be able to help you. We'll need to see what node you attach the listener to, essentially what happens before you call showData. We'll also want to see the JSON at the location you listen to, as text (no screenshots please). You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your Firebase Database console. Commented Feb 6, 2020 at 20:13

2 Answers 2

1

Your arrayList is of type PatientInformation so I think you could just add patientInfo which is of type PatientInformation also.

private void showData(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
    PatientInformation patientInfo = new PatientInformation();
    patientInfo.setName(ds.child(userID).getValue(PatientInformation.class).getName());
    patientInfo.setAge(ds.child(userID).getValue(PatientInformation.class).getAge());
    patientInfo.setDate(ds.child(userID).getValue(PatientInformation.class).getDate());
    ArrayList<PatientInformation> arrayList = new ArrayList<>();
    arrayList.add(patientInfo);

    PatientInformationAdapter pAdapter = new PatientInformationAdapter(this, arrayList);
    listView.setAdapter(pAdapter);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Is the userID valid? Because if not it may produce a null PatientInformation object. Check it cause it might be a problem
1

You're creating a new array list and adapter for every child node of the results. You probably want to create only one array list and adapter, like this:

private void showData(DataSnapshot dataSnapshot) {
    ArrayList<PatientInformation> arrayList = new ArrayList<>();
    for (DataSnapshot ds : dataSnapshot.getChildren()) {
        PatientInformation patientInfo = ds.child(userID).getValue(PatientInformation.class)
        arrayList.add(patientInfo);
    }
    PatientInformationAdapter pAdapter = new PatientInformationAdapter(this, arrayList);
    listView.setAdapter(pAdapter);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.