我不太确定什么时候出错了,但我检查了变量名是否相同,无论是在布局 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" />
最佳答案
我的理解是dataSnapshot
为NULL。您应该通过在调试器中运行代码并在此行放置一个断点来确保该变量不为空。
也许您可以向我们提供更大范围的代码,以便我们可以了解 dataSnapshot
的来源。
关于java.lang.NullPointerException : Attempt to read from field 'java.lang.String com.example.g.Model.PersonalShopper.ps_name' on a null object reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58398322/