【问题标题】:get all table values from firebase null object reference firebase database [duplicate]从firebase空对象参考firebase数据库中获取所有表值[重复]
【发布时间】:2019-01-06 01:21:07
【问题描述】:
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 );

这是图片中的错误

我想获取bpfragmentTable 所有值并将所有值存储在字符串变量中,并使用该变量在图形视图中绘制图形。我想从图中的table-BPFragment表中获取所有值:

这是 BPFragmentTable.class 的 getter 和 setter 类

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

为什么我得到空对象引用?

【问题讨论】:

    标签: android firebase-realtime-database nullreferenceexception null-object-pattern


    【解决方案1】:

    数据是从 Firebase 异步加载的。当您调用 formatter.parse(datebp) 时,onDataChange 方法尚未运行,并且 datebp 尚未设置。您需要将数据解析移至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
        }
    });
    

    这种异步特性对于刚接触 Firebase 的开发人员来说是一个常见的陷阱。由于异步 API 在现代编程中极为常见,我强烈建议您快速了解它们。有关一些好的介绍性材料,请参阅:

    【讨论】:

    • 非常感谢您的回答先生,我正在按照您所说的应用更改
    • 没有先生,当我将它 formatter.parse(datebp) 放在 onDataChange 中时,同样的错误空对象引用意味着 system.out.println 中的仍然是空值
    • 我认为我的解决方案在您的第一个链接 (getContactsFromFirebase()) 中,我正在阅读它,现在我明白为什么我不断收到空值,但我需要根据我的代码制作该代码
    • 非常感谢你的链接帮助了我很多,我现在因为回调方法得到了一个空值
    • 实际上,先生,当您给我第一个链接时,我点击了赞成票,它没有显示我的投票,因为我是 Stack OverFlow 的新手,我必须有 15 个声望才能投票给任何答案......我很抱歉先生,非常感谢您的大力帮助,非常感谢您,先生,我永远不会忘记您的帮助,愿您长寿,美好生活,先生
    猜你喜欢
    • 2020-12-22
    • 1970-01-01
    • 2016-12-21
    • 2018-06-23
    • 2018-03-17
    • 2018-10-03
    • 2022-07-01
    • 2016-12-10
    • 2018-02-10
    相关资源
    最近更新 更多