[英]java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.example.g.Model.PersonalShopper.ps_name' on a null object reference
I don't quite sure what when wrong, but I have check the variable name is the same whether it is in a layout xml and the java class, but when I run the apps it give me error which said that我不太确定什么时候出错了,但是我检查了变量名称是否相同,无论它是否在布局 xml 和 java class 中,但是当我运行应用程序时它给出了错误
java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.example.g.Model.Ps.ps_name' on a null object reference java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.example.g.Model.Ps.ps_name' on a null object reference
I have declare the variable btw.我已经声明了变量顺便说一句。
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" />
My understanding is that the dataSnapshot
is NULL.我的理解是dataSnapshot
是NULL。 You should make sure that this variable is not null by lunching your code in debugger and put a breakpoint at this line.你应该确保这个变量不是 null 通过在调试器中启动你的代码并在这一行放置一个断点。
Maybe you could give us a larger scope of your code so we can see where dataSnapshot
comes from.也许你可以给我们一个更大的 scope 代码,这样我们就可以看到dataSnapshot
的来源。
ps
is null: ps
是 null:
PersonalShopper ps = dataSnapshot.getValue(PersonalShopper.class);
This means there was no data at the location of the database that you queried.这意味着您查询的数据库位置没有数据。 Check your query and the contents of the database to make sure everything is correct.检查您的查询和数据库的内容以确保一切正确。
ps is null: So check if the child have value or not. ps 是 null:所以检查孩子是否有价值。 You can also check the below method it will stop app from crashing and if child value is not null it will show the data.您还可以检查以下方法,它将阻止应用程序崩溃,如果子值不是 null,它将显示数据。
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
PersonalShopper ps = dataSnapshot.getValue(PersonalShopper.class);
psname.setText(ps.ps_name);
pspassword.setText(ps.ps_password);
psemail.setText(ps.ps_email);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.