java - 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" />
解决方案
我的理解是dataSnapshot
NULL。您应该通过在调试器中启动您的代码并在此行放置一个断点来确保此变量不为空。
也许你可以给我们更大范围的代码,这样我们就可以看到dataSnapshot
来自哪里。
推荐阅读
- coq - 如何向 Coq 传达某些类型是相等的?
- yaml - 不使用时从 Swagger 禁用授权方案
- r - 如何在我的应用程序的面板部分应用自动滚动?
- r - 根据其他数据帧中指定的不同时间范围对数据帧进行子集
- java - 更新 java 中的 sqlite_sequence 表
- recursion - clang 是否支持 CUDA __global__ 函数递归?
- java - jax-RS 项目部署成功,但使用 IE 打开 URL 时出现 404 错误
- c# - 监听服务器端 C# 代码中活动的更新
- ios - "unrecognized selector sent to instance" when selector is in other file
- php - 我的照片没有上传到数据库和文件