childEventListener抛出nullPointerException [英] childEventListener throws nullPointerException

查看:41
本文介绍了childEventListener抛出nullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firebase数据库中有一个要检索的节点,并将其放入微调器中,我制作了数组列表,然后遍历了数据快照,但仍然给出nullPointerException我使用push方法将子级的值存储在Firebase中

I have a node in firebase database that I want to retrieve and put it in a spinner I made array list then looped through data snapshot but still give nullPointerException I stored the value of the child in firebase using push method

我尝试了一切,但是没有用

I tried everything but it didn't work

模型类

public class Purpose {

private String name;

public Purpose(){

}

public Purpose(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
 }

添加数据类别

        puposedRef = FirebaseDatabase.getInstance().getReference().child("Purposes");

    saveBtn = findViewById(R.id.add_purpose_save_btn);

    saveBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String name = addNameEd.getText().toString();

            Map<String , String> purposeMap = new HashMap<>();
            purposeMap.put("name" , name);


            puposedRef.push().setValue(purposeMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if(task.isSuccessful()){
                        Toast.makeText(AddPurposeActivity.this, "Added to database", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(AddPurposeActivity.this, 
          task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            });

        }
    });

检索课程

        puposedRef = FirebaseDatabase.getInstance().getReference().child("Purposes");

    textView = findViewById(R.id.show_purpose);

    puposedRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

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

                String key = puposedRef.push().getKey();
                String name = ds.child(key).getValue(Purpose.class).getName();
                textView.setText(name);

            }


        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

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

                String key = puposedRef.push().getKey();
                String name = ds.child(key).getValue(Purpose.class).getName();
                textView.setText(name);

            }

        }


        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

logcat

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.best.karem.mycareassociatesadmin.Model.Purpose.getName()' on a null object reference
    at com.best.karem.mycareassociatesadmin.AddPurposeActivity$1.onChildAdded(AddPurposeActivity.java:52)

推荐答案

您要在目的上附加 ChildEventListener .这意味着它的回调方法( onChildAdded onChildChanged 等)是通过 Purposes 子节点的 DataSnapshot 调用的.代码>.

You're attaching a ChildEventListener on Purposes. That means that its callback methods (onChildAdded, onChildChanged, etc) are called with a DataSnapshot of a child node of Purposes.

在您的 onChildAdded 中,您正在遍历该快照的子节点,这意味着 ds 是单个属性 name .

In your onChildAdded you're looping over the child nodes of that snapshot, which means that ds is a snapshot of an individual property name.

要解决此问题,请不要在 onChild * 方法中遍历子节点:

To fix the problem, don't loop over the child nodes in your onChild* methods:

public void onChildAdded(DataSnapshot dataSnapshot, String previousChildKey) {
    String name = dataSnapshot.getValue(Purpose.class).getName();
    textView.setText(name);
}

这篇关于childEventListener抛出nullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆