猿问

NullPointerException 从 Firebase 数据库中检索数据

我正在尝试从 Firebase 数据库中检索 9 个布尔变量和一个 int 变量,但是每次我运行我的应用程序时,它都会给我一个空指针异常


它来自一个 Android 应用程序,我设置了一个模型页面,我设置了 getter 和 setter 方法,但它一直在检索空指针 exeption 这是模型


public class ButtonInformation {

    private boolean Button1=false;

    private boolean Button2=false;

    private boolean Button3=false;

    private boolean Button4=false;

    private boolean Button5=false;

    private boolean Button6=false;

    private boolean Button7=false;

    private boolean Button8=false;

    private boolean Button9=false;

    private int ApeOmaggio=0;



    public ButtonInformation(){

    }


    public ButtonInformation(boolean button1, boolean button2, boolean button3, boolean button4, boolean button5, boolean button6, boolean button7, boolean button8, boolean button9, int apeOmaggio) {

        Button1 = button1;

        Button2 = button2;

        Button3 = button3;

        Button4 = button4;

        Button5 = button5;

        Button6 = button6;

        Button7 = button7;

        Button8 = button8;

        Button9 = button9;

        ApeOmaggio = apeOmaggio;

    }


    public boolean getButton1(){

        return Button1;

    }


    public void setButton1(boolean Button1){

        this.Button1 = Button1;

    }


    public boolean getButton2(){

        return Button2;

    }


    public void setButton2(boolean Button2){

        this.Button2 = Button2;

    }


    public boolean getButton3(){

        return Button3;

    }


    public void setButton3(boolean Button3){

        this.Button3 = Button3;

    }


    public boolean getButton4(){

        return Button4;

    }


    public void setButton4(boolean Button4){

        this.Button4 = Button4;

    }


    public boolean getButton5(){

        return Button5;

    }


    public void setButton5(boolean Button5){

        this.Button5 = Button5;

    }


    public boolean getButton6(){

        return Button6;

    }


    public void setButton6(boolean Button6){

        this.Button6 = Button6;

    }


    public boolean getButton7(){

        return Button7;

    }



侃侃无极
浏览 93回答 2
2回答

慕姐4208626

您可以直接将您的模型类与 Firebase 响应数据进行映射。您不必手动设置模型类中的每个数据。如果您的数据将从 Firebase 中成功检索,则此代码将起作用,请尝试以下代码。@Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_clienti);        Utente=findViewById(R.id.tvUtente);        ApeOmaggio = findViewById(R.id.tvOmaggio);        bInfo= new ButtonInformation();        img1=findViewById(R.id.img1);        img2=findViewById(R.id.img2);        img3=findViewById(R.id.img3);        img4=findViewById(R.id.img4);        img5=findViewById(R.id.img5);        img6=findViewById(R.id.img6);        img7=findViewById(R.id.img7);        img8=findViewById(R.id.img8);        img9=findViewById(R.id.img9);        mAuth=FirebaseAuth.getInstance();        mFirebaseDatabase = FirebaseDatabase.getInstance();        user = mAuth.getCurrentUser();        utenteLog = user.getDisplayName();        mail = user.getEmail();        mailNoSpace=mail.replaceAll("\\.","");        Utente.setText(utenteLog);        myRef=mFirebaseDatabase.getReference(mailNoSpace);        myRef.addValueEventListener(new ValueEventListener()        {            @Override            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {            ButtonInformation buttonInformation = dataSnapshot.getValue(ButtonInformation.class);                showData(buttonInformation);            }            @Override            public void onCancelled(@NonNull DatabaseError databaseError) {            }        });    }    private void showData(ButtonInformation buttonInformation) {                    if (buttonInformation.getButton1()==true){                img1.setImageResource(R.drawable.berry_icon);            }else{                img1.setImageResource(R.drawable.radio_button);            }            if (buttonInformation.getButton2()==true){                img2.setImageResource(R.drawable.berry_icon);            }else{                img2.setImageResource(R.drawable.radio_button);            }            if (buttonInformation.getButton3()==true){                img3.setImageResource(R.drawable.berry_icon);            }else{                img3.setImageResource(R.drawable.radio_button);            }            if (buttonInformation.getButton4()==true){                img4.setImageResource(R.drawable.berry_icon);            }else{                img4.setImageResource(R.drawable.radio_button);            }            if (buttonInformation.getButton5()==true){                img5.setImageResource(R.drawable.berry_icon);            }else{                img5.setImageResource(R.drawable.radio_button);            }            if (buttonInformation.getButton6()==true){                img6.setImageResource(R.drawable.berry_icon);            }else{                img6.setImageResource(R.drawable.radio_button);            }            if (buttonInformation.getButton7()==true){                img7.setImageResource(R.drawable.berry_icon);            }else{                img7.setImageResource(R.drawable.radio_button);            }            if (buttonInformation.getButton8()==true){                img8.setImageResource(R.drawable.berry_icon);            }else{                img8.setImageResource(R.drawable.radio_button);            }            if (buttonInformation.getButton9()==true){                img9.setImageResource(R.drawable.berry_icon);            }else{                img9.setImageResource(R.drawable.radio_button);            }            ApeOmaggio.setText(Integer.toString(bInfo.getApeOmaggio()));    }

牛魔王的故事

在访问 firebase 之前使用 if not null 条件。或者,如果您已将子属性更改为您的 firebase 项目,请检查树。
随时随地看视频慕课网APP

相关分类

Java
我要回答