简体   繁体   English

java.lang.NullPointerException:尝试调用虚拟方法+ Firebase

[英]java.lang.NullPointerException: Attempt to invoke virtual method + Firebase

I'm trying to get the data from a database in Firebase in a Fragment class I need to get the name and email (nombre, correo) using DataSnapshot, from a firebase database. 我正在尝试从Fragment类的Firebase数据库中获取数据,我需要使用FireSnap数据库中的DataSnapshot来获取名称和电子邮件(nombre,correo)。 I use userID = currentUser.getUid() to find the data associate, but I have a Error java.lang.NullPointerException: Attempt to invoke virtual method 我使用userID = currentUser.getUid()来查找数据关联,但是我遇到错误java.lang.NullPointerException:尝试调用虚拟方法

public class ProfileFragment extends Fragment {

    private FirebaseAuth mAuth;
    private FirebaseDatabase database;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private DatabaseReference myRef;
    private DatabaseReference myNewRef;


    //Data Firebase
    private String userID;
    private String email;



    public ProfileFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        myRef = FirebaseDatabase.getInstance().getReference();
        myNewRef = myRef.child("Usuarios");

        mAuth = FirebaseAuth.getInstance();
        database = FirebaseDatabase.getInstance();//Usuarios siempre con mayusculas

        final FirebaseUser currentUser = mAuth.getCurrentUser();
        userID = currentUser.getUid();
        email = currentUser.getEmail();
        DatabaseReference reference = database.getReference();


        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser currentUser1 = firebaseAuth.getCurrentUser();
                if(currentUser != null){
                    Log.d(TAG, "signed in. " + currentUser.getUid());
                }else{
                    Log.d(TAG, "Signed out");
                }
            }
        };

        myNewRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                showData(dataSnapshot);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });



        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_profile, container, false);
        showToolbar("",false,view);
        RecyclerView cardsRecycler = (RecyclerView) view.findViewById(R.id.pictureProfileRecycler);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        cardsRecycler.setLayoutManager(linearLayoutManager);
        CardAdapterRecyclerView cardAdapterRecyclerView = new CardAdapterRecyclerView(buildCard(),R.layout.cardview_category,getActivity());
        cardsRecycler.setAdapter(cardAdapterRecyclerView);


        return view;
    }


    public void showData(DataSnapshot dataSnapshot){
        for(DataSnapshot ds: dataSnapshot.getChildren()){
            UserInformation userInformation = new UserInformation();

            Log.d(TAG, "USER ID " + userID);

            userInformation.setNombre(ds.child(userID).getValue(UserInformation.class).getNombre());
            userInformation.setCorreo(ds.child(userID).getValue(UserInformation.class).getCorreo());

            Log.d(TAG, "name " + userInformation.getNombre());
            Log.d(TAG, "email " + userInformation.getCorreo());
        }
    }

    @Override
    public void onStart(){
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    public void onStop(){
        super.onStop();
        if(mAuthListener != null){
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }


    public void showToolbar(String tittle, boolean upButton, View view){

        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(tittle);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(upButton);
    }

}

I have this result 我有这个结果

FATAL EXCEPTION: main
Process: biz.kapta.desafioautomotriz, PID: 2815
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String biz.kapta.desafioautomotriz.model.UserInformation.getNombre()' on a null object reference

I already have the model "UserInformation" 我已经有了模型“ UserInformation”

public class UserInformation {
    private String nombre;
    private String correo;

    public UserInformation(){

    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getCorreo() {
        return correo;
    }

    public void setCorreo(String correo) {
        this.correo = correo;
    }

}


          [Firebase database][1]                                                      

Looks like this line is a problem right ? 看起来这行是个问题吧?

userInformation.setNombre(ds.child(userID).getValue(UserInformation.class).getNombre());

ds is probably null, try log ds value and see if it's null ds可能为空,请尝试记录ds的值,看看是否为空

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio java.lang.NullPointerException:尝试调用虚拟方法 - Android Studio java.lang.NullPointerException: Attempt to invoke virtual method TabLayout 中的“java.lang.NullPointerException:尝试调用虚拟方法” - “java.lang.NullPointerException: Attempt to invoke virtual method” in TabLayout java.lang.NullPointerException:尝试调用虚拟方法“com.google.firebase.database” - java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database Android Studio -(java.lang.NullPointerException:尝试调用虚拟方法'com.google.firebase.database.ChildEventListener') - Android Studio - (java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener') java.lang.NullPointerException:尝试在 oncreate 方法中对空对象引用调用虚拟方法 - java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference in oncreate method java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法… - java.lang.NullPointerException: Attempt to invoke virtual method … on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“RecyclerView$ViewHolder.shouldIgnore()” - java.lang.NullPointerException: Attempt to invoke virtual method 'RecyclerView$ViewHolder.shouldIgnore()' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚方法'ActionBar.setNavigationMode(int)' - java.lang.NullPointerException: Attempt to invoke virtual method 'ActionBar.setNavigationMode(int)' on a null object reference 致命异常:java.lang.NullPointerException:尝试在空对象引用onClickItem上调用虚方法void - FATAL EXCEPTION: java.lang.NullPointerException: Attempt to invoke virtual method void on a null object reference onClickItem Android 应用程序不断崩溃原因:java.lang.NullPointerException:尝试调用虚拟方法 - Android App Keep Crashing Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM