简体   繁体   English

java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.example.g.Model.PersonalShopper.ps_name' on a null object reference

[英]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的来源。

This link may help you.. 这个链接可以帮助你..

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.

相关问题 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.gson.JsonElement.toString()' on a null object reference - java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.gson.JsonElement.toString()' on a null object reference Android:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.String java.lang.Object.toString()” - Android: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference java.lang.NullPointerException:尝试在GridView中的空对象引用上调用虚方法&#39;java.lang.String java.lang.Object.toString()&#39; - java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference in GridView java.lang.NullPointerException:尝试从空对象引用中的字段“ long org.opencv.core.Mat.nativeObj”读取 - java.lang.NullPointerException: Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法&#39;int org.json.JSONObject.getInt(java.lang.String)&#39; - java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONObject.getInt(java.lang.String)' on a null object reference NullPointerException:尝试调用接口方法:在 null object 参考上的 OnDatabaseCallback.insert(java.lang.String)' - NullPointerException: Attempt to invoke interface method: OnDatabaseCallback.insert(java.lang.String)' on a null object reference Attempt to invoke virtual method 'java.lang.String com.dantools.OurChat.Model.User.getUsername()' on a null object reference - Attempt to invoke virtual method 'java.lang.String com.dantools.OurChat.Model.User.getUsername()' on a null object reference 引起:java.lang.NullPointerException: Attempt to invoke virtual method &#39;boolean java.lang.String.isEmpty()&#39; on a null object reference - Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 &#39;char[] java.lang.String.toCharArray()&#39; - java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference 尝试在 null object 参考上调用虚拟方法“java.lang.String Model.Users.getName()” - Attempt to invoke virtual method 'java.lang.String Model.Users.getName()' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM