get all table values from firebase null object reference firebase database [duplicate]
Asked Answered
P

1

2
public class ShowBPGraphActivity extends AppCompatActivity {
public GraphView graphView;
public LineGraphSeries <DataPoint>lineGraphSeries; 
public String systolicbp;
public String diastolicbp;
public String timebp;
public String datebp;

public DatabaseReference db;
public BPFragmentTable bpFragmentTable = new BPFragmentTable(  );

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_show_bp_graph );
    ButterKnife.bind( this );

     db = FirebaseDatabase.getInstance().getReference("bpfragmentTable");

    final GraphView graph = (GraphView) findViewById( R.id.graphView );

    final List<BPFragmentTable> bpFragmentTableList = new ArrayList<>();

    //Get datasnapshot at your "users" root node
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("bpfragmentTable");
    ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    List<BPFragmentTable> bpfragmentTableList = new ArrayList<>();

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

                        BPFragmentTable bpfragmentTable = dataSnapshot1.getValue(BPFragmentTable.class);

                        systolic = bpfragmentTable.getSystolicbp();
                        diastolic = bpfragmentTable.getDiastolicbp();
                        date = bpfragmentTable.getDatebp();
                        time = bpfragmentTable.getTimebp();
                        //bpfragmentTable.setSystolicbp(systolic);
                        //bpfragmentTable.setDiastolicbp(diastolic);
                        //bpfragmentTable.setDatebp(date);
                        //bpfragmentTable.setTimebp(time);
                        bpfragmentTableList.add(bpfragmentTable);
                        // Toast.makeText(MainActivity.this,""+name,Toast.LENGTH_LONG).show();
                    }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    //handle databaseError
                }
            });
// please help me why i get null value in systolic diastolic date and time

            System.out.println( "systolicbp" + systolicbp );
            System.out.println( "diastolicbp" + diastolicbp );
            System.out.println( "datebp" + datebp );
            System.out.println( "timebp" + timebp );

    try {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a\nMMM dd yyyy");
        dateValued = formatter.parse(datebp);
        dateInMills = dateValued.getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }

    final long xlong = dateInMills;
    final int ysystolicInt  = Integer.parseInt( systolicbp );
    final int ydiastolicInt = Integer.parseInt( diastolicbp );

this is the error in the picture

getting null object reference from firebase database

I want to get bpfragmentTable all values and store all values in the string variable and use that variable to draw a graph in the graph view. I want to get all values from the table-BPFragment table in the picture:

FireBase DataBase bpfragmentTable

and this is the class of getter and setter of BPFragmentTable.class

public class BPFragmentTable {

public String bpid;
public String systolicbp;
public String diastolicbp;
public String datebp;
public String timebp;

public BPFragmentTable(){}
public BPFragmentTable(String bpid,String systolicbp,String diastolicbp,String datebp,String timebp) {

     this.bpid = bpid;
     this.systolicbp = systolicbp;
     this.diastolicbp = diastolicbp;
     this.datebp = datebp;
     this.timebp = timebp;

}

public String getBpid() {
    return bpid;
}

public void setBpid(String  bpid) {
    this.bpid = bpid;
}

public String getSystolicbp() {
    return systolicbp;
}

public void setSystolicbp(String systolicbp) {
    this.systolicbp = systolicbp;
}

public String getDiastolicbp() {
    return diastolicbp;
}

public void setDiastolicbp(String diastolicbp) {
    this.diastolicbp= diastolicbp;
}

public String getDatebp() {
    return datebp;
}

public void setDatebp(String datebp) {
    this.datebp= datebp;
}

public String getTimebp() {
    return timebp;
}

public void setTimebp(String timebp) {
    this.timebp= timebp;
} 
}

Why do I get null object reference?

Pintsize answered 29/7, 2018 at 17:48 Comment(0)
E
2

Data is loaded from Firebase asynchronously. By the time you call formatter.parse(datebp) the onDataChange method hasn't run yet, and datebp hasn't been set. You will need to move the parsing of the data into onDataChange:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("bpfragmentTable");
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<BPFragmentTable> bpfragmentTableList = new ArrayList<>();

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

            BPFragmentTable bpfragmentTable = dataSnapshot1.getValue(BPFragmentTable.class);

            systolic = bpfragmentTable.getSystolicbp();
            diastolic = bpfragmentTable.getDiastolicbp();
            date = bpfragmentTable.getDatebp();
            time = bpfragmentTable.getTimebp();
            bpfragmentTableList.add(bpfragmentTable);

            System.out.println( "systolicbp" + systolicbp );
            System.out.println( "diastolicbp" + diastolicbp );
            System.out.println( "datebp" + datebp );
            System.out.println( "timebp" + timebp );

            try {
                @SuppressLint("SimpleDateFormat") SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a\nMMM dd yyyy");
                dateValued = formatter.parse(datebp);
                dateInMills = dateValued.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
});

This asynchronous nature is a common pitfall for developers new to Firebase. Since asynchronous APIs are extremely common in modern programming, I highly recommend you learn about them quickly. For some good introductory material, see:

Esqueda answered 29/7, 2018 at 17:56 Comment(5)
thank you so much respectful sir for your answer and i am applying the change as you saidPintsize
no sir when i put it formatter.parse(datebp) inside the onDataChange then same error null object reference mean still null values in system.out.printlnPintsize
i think my solution is inside your first link (getContactsFromFirebase()) i am reading it and now i understand that why i keep getting null values and but i need to make that code acording to my codePintsize
thank you so much your link helped me alot and i now i did get a null value because of call back methodPintsize
acctually sir i clicked the up vote when you give me the first link it did not show my vote because i am new in Stack OverFlow and i must have 15 reputation to up Vote any answer ... i am so sorry sir and thank you so much for the big big big help thank you so much sir i never forget your help May you live long and good life SirPintsize

© 2022 - 2024 — McMap. All rights reserved.