Null Pointer Exeption从Firebase数据库检索数据

时间:2019-02-07 09:18:46

标签: java android firebase firebase-realtime-database

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

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

;

这是主要活动

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

    public void setButton7(boolean Button7){
        this.Button7 = Button7;
    }

    public boolean getButton8(){
        return Button8;
    }

    public void setButton8(boolean Button8){
        this.Button8 = Button8;
    }

    public boolean getButton9(){
        return Button9;
    }

    public void setButton9(boolean Button9){
        this.Button9 = Button9;
    }

    public int getApeOmaggio(){
        return ApeOmaggio;
    }

    public void setApeOmaggio(int ApeOmaggio){
        this.ApeOmaggio = ApeOmaggio;
    }
}

这是显示的错误消息

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.vavasimo.berrycoffeebardrinks.Model.ButtonInformation;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class ActivityClienti extends AppCompatActivity{
    private static final String CHAT_PREFS = "Chat prefs";
    private static final String NOME_KEY = "username";
    FirebaseAuth mAuth;
    private FirebaseDatabase mFirebaseDatabase;
    private DatabaseReference myRef;
    private String mail;
    private String mailNoSpace;
    TextView Utente;
    TextView ApeOmaggio;
    ImageView img1,img2,img3,img4,img5,img6,img7,img8,img9;
    ButtonInformation bInfo;
   FirebaseUser user;
           String utenteLog;

    @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) {
                showData(dataSnapshot);
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    }

    private void showData(DataSnapshot dataSnapshot) {
        for (DataSnapshot ds : dataSnapshot.getChildren()){
            bInfo = new ButtonInformation();
            try{
            bInfo.setButton1(ds.child(mailNoSpace).getValue(ButtonInformation.class).getButton1());
            bInfo.setButton2(ds.child(mailNoSpace).getValue(ButtonInformation.class).getButton2());
            bInfo.setButton3(ds.child(mailNoSpace).getValue(ButtonInformation.class).getButton3());
            bInfo.setButton4(ds.child(mailNoSpace).getValue(ButtonInformation.class).getButton4());
            bInfo.setButton5(ds.child(mailNoSpace).getValue(ButtonInformation.class).getButton5());
            bInfo.setButton6(ds.child(mailNoSpace).getValue(ButtonInformation.class).getButton6());
            bInfo.setButton7(ds.child(mailNoSpace).getValue(ButtonInformation.class).getButton7());
            bInfo.setButton8(ds.child(mailNoSpace).getValue(ButtonInformation.class).getButton8());
            bInfo.setButton9(ds.child(mailNoSpace).getValue(ButtonInformation.class).getButton9());
            bInfo.setApeOmaggio(ds.child(mailNoSpace).getValue(ButtonInformation.class).getApeOmaggio());}
            catch (NullPointerException e){
                Toast.makeText(this,"Aiuto",Toast.LENGTH_LONG).show();
            }

            if (bInfo.getButton1()==true){
                img1.setImageResource(R.drawable.berry_icon);
            }else{
                img1.setImageResource(R.drawable.radio_button);
            }
            if (bInfo.getButton2()==true){
                img2.setImageResource(R.drawable.berry_icon);
            }else{
                img2.setImageResource(R.drawable.radio_button);
            }
            if (bInfo.getButton3()==true){
                img3.setImageResource(R.drawable.berry_icon);
            }else{
                img3.setImageResource(R.drawable.radio_button);
            }
            if (bInfo.getButton4()==true){
                img4.setImageResource(R.drawable.berry_icon);
            }else{
                img4.setImageResource(R.drawable.radio_button);
            }
            if (bInfo.getButton5()==true){
                img5.setImageResource(R.drawable.berry_icon);
            }else{
                img5.setImageResource(R.drawable.radio_button);
            }
            if (bInfo.getButton6()==true){
                img6.setImageResource(R.drawable.berry_icon);
            }else{
                img6.setImageResource(R.drawable.radio_button);
            }
            if (bInfo.getButton7()==true){
                img7.setImageResource(R.drawable.berry_icon);
            }else{
                img7.setImageResource(R.drawable.radio_button);
            }
            if (bInfo.getButton8()==true){
                img8.setImageResource(R.drawable.berry_icon);
            }else{
                img8.setImageResource(R.drawable.radio_button);
            }
            if (bInfo.getButton9()==true){
                img9.setImageResource(R.drawable.berry_icon);
            }else{
                img9.setImageResource(R.drawable.radio_button);
            }
            ApeOmaggio.setText(Integer.toString(bInfo.getApeOmaggio()));

        }
    }







    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.layout_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id==R.id.log_out_item){
            mAuth.signOut();
            Intent intent = new Intent (ActivityClienti.this, LogInActivity.class);
            finish();
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }

    }

2 个答案:

答案 0 :(得分:0)

在访问Firebase之前使用if(如果不为null)条件。 或者,如果您将子属性更改为Firebase项目,请检查树。

答案 1 :(得分:0)

您可以直接使用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()));

    }