无法从ValueEventListener内部类读取值

时间:2021-11-23 23:28:52

I try to get single record from my Firebase database and convert it as an object from Kategori class, determined by its id_kategor' attributes.

我尝试从我的Firebase数据库中获取单个记录,并将其转换为Kategori类中的对象,由其id_kategor属性确定。

public class Kategori {

  private String nama_id;
  private String nama_en;
  private String id_kategori;
  private Integer jml_item = 0;
  private Integer jml_brand = 0;

  public Kategori(){}

  public Kategori(String id_kategori) {
    this.id_kategori = id_kategori;
    this.fetchData();
  }

  private void fetchData(){
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("kategori");

    myRef.child( this.id_kategori ).addListenerForSingleValueEvent(new ValueEventListener (){

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if( dataSnapshot.exists() ){
                String nama_en = dataSnapshot.child("nama_en").getValue(String.class);
                String nama_id = dataSnapshot.child("nama_id").getValue(String.class);

                Kategori.this.nama_en = nama_en;
                Kategori.this.nama_id = nama_id;

                Log.d(">>>>>> CHECK 1 >>>>>",Kategori.this.nama_en);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    //Log.d(">>>>>> CHECK 2 >>>>>",this.nama_en);
  }
}

I expect all attributes can get its correct value from Firebase.

我希望所有属性都可以从Firebase获得正确的值。

Logs with tag CHECK 1 shows the correct result that means successfully get its values from Firebase. But logs with tag CHECK 2 gives me run-time error *"java.lang.NullPointerException: println needs a message"*, that means **'nama_en'** attribute has *null* value, and also means the attribut failed to get it's value from Firebase ValueEventListener

带有标记CHECK 1的日志显示正确的结果,这意味着从Firebase成功获取其值。但是带有标签CHECK 2的日志给我运行时错误*“java.lang.NullPointerException:println需要一条消息”*,这意味着**'nama_en'**属性有* null *值,也意味着attribut失败了从Firebase ValueEventListener中获取它的价值

What is exactly the problem?

究竟是什么问题?

Here is my data samples

这是我的数据样本

无法从ValueEventListener内部类读取值

1 个解决方案

#1


1  

Check 2 might run before Check 1 and especially before nama_en is initialized.

检查2可能在检查1之前运行,特别是在初始化nama_en之前。

The point here is that addListenerForSingleValueEvent adds the inner class as listener, but it does not immediately execute the code in it. Instead it directly continues with the Check 2 debug statement.
In particular name_en is initialized onDataChange, hence when the method is called, which should happen when whatever data is changed (I'm not familiar with Firebase API).

这里的要点是addListenerForSingleValueEvent将内部类添加为侦听器,但它不会立即执行其中的代码。而是直接继续使用Check 2调试语句。特别是name_en在onDataChange上被初始化,因此当调用该方法时,这应该在任何数据发生变化时发生(我不熟悉Firebase API)。

You can check this behavior by activating both debug statements at once and maybe use String.valueOf(Object o), so you don't run into NPEs, but still see the null value.

您可以通过立即激活两个调试语句并使用String.valueOf(Object o)来检查此行为,因此您不会遇到NPE,但仍会看到null值。

#1


1  

Check 2 might run before Check 1 and especially before nama_en is initialized.

检查2可能在检查1之前运行,特别是在初始化nama_en之前。

The point here is that addListenerForSingleValueEvent adds the inner class as listener, but it does not immediately execute the code in it. Instead it directly continues with the Check 2 debug statement.
In particular name_en is initialized onDataChange, hence when the method is called, which should happen when whatever data is changed (I'm not familiar with Firebase API).

这里的要点是addListenerForSingleValueEvent将内部类添加为侦听器,但它不会立即执行其中的代码。而是直接继续使用Check 2调试语句。特别是name_en在onDataChange上被初始化,因此当调用该方法时,这应该在任何数据发生变化时发生(我不熟悉Firebase API)。

You can check this behavior by activating both debug statements at once and maybe use String.valueOf(Object o), so you don't run into NPEs, but still see the null value.

您可以通过立即激活两个调试语句并使用String.valueOf(Object o)来检查此行为,因此您不会遇到NPE,但仍会看到null值。