首页 > 解决方案 > java.lang.NullPointerException:尝试从空对象引用上的字段“java.lang.String com.example.g.Model.PersonalShopper.ps_name”读取

问题描述

我不太确定什么时候出错了,但是我检查了变量名是否相同,无论它是在布局 xml 还是 java 类中,但是当我运行应用程序时,它给了我错误,说

java.lang.NullPointerException:尝试从空对象引用上的字段“java.lang.String com.example.g.Model.Ps.ps_name”读取

我已经声明了变量顺便说一句。

 firebaseAuth = FirebaseAuth.getInstance();
        currentUser = firebaseAuth.getCurrentUser();
        psID = currentUser.getUid();
        databaseReference = FirebaseDatabase.getInstance().getReference("PersonalShopper").child(firebaseAuth.getUid());

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                PersonalShopper ps = dataSnapshot.getValue(PersonalShopper.class);
                psname.setText(ps.ps_name);
                pspassword.setText(ps.ps_password);
                psemail.setText(ps.ps_email);

            }



 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textSize="13dp"/>

    <EditText
        android:id="@+id/psname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <TextView
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Password"
        android:textSize="13dp"/>


    <EditText
        android:id="@+id/pspassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <TextView
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Email"
        android:textSize="13dp"/>

    <TextView
        android:id="@+id/psemail"
        android:hint="email@gmail.com"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />



标签: javaandroidfirebase

解决方案


我的理解是dataSnapshotNULL。您应该通过在调试器中启动您的代码并在此行放置一个断点来确保此变量不为空。

也许你可以给我们更大范围的代码,这样我们就可以看到dataSnapshot来自哪里。

这个链接可以帮助你..


推荐阅读