java - Firebase 数据库数据检索在空对象引用上给出 NullPointerException 'java.lang.String java.lang.Object.toString()'
问题描述
我有一个 Firebase 数据库,供两个 Android 项目使用。我正在尝试通过遵循 YouTube 上的各种教程从一个项目/应用程序中添加数据并在另一个项目/应用程序中检索数据。但是,我很难检索数据。这是我的数据库的快照。
我的应用程序加载 UI,但在我运行以下代码并单击按钮后崩溃Get Data
。
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import org.w3c.dom.Attr;
import java.util.jar.Attributes;
import java.util.*;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class Main2Activity extends AppCompatActivity {
DatabaseReference reff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
final TextView text1=(TextView)findViewById(R.id.textView);
final TextView text2=(TextView)findViewById(R.id.textView2);
final TextView text3=(TextView)findViewById(R.id.textView3);
final TextView text4=(TextView)findViewById(R.id.textView4);
final TextView text5=(TextView)findViewById(R.id.textView5);
final TextView text6=(TextView)findViewById(R.id.textView6);
final TextView text7=(TextView)findViewById(R.id.textView7);
final TextView text8=(TextView)findViewById(R.id.textView8);
final TextView text9=(TextView)findViewById(R.id.textView9);
final TextView text10=(TextView)findViewById(R.id.textView10);
final TextView text11=(TextView)findViewById(R.id.textView11);
final TextView text12=(TextView)findViewById(R.id.textView12);
final TextView text13=(TextView)findViewById(R.id.textView13);
final TextView text14=(TextView)findViewById(R.id.textView14);
Button btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
reff= FirebaseDatabase.getInstance().getReference().child("Attributes").child("M0pSXmPNPbXa3vgQeuc");
reff.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int attribute1=Integer.parseInt(dataSnapshot.child("attribute1").getValue().toString());
int attribute2=Integer.parseInt(dataSnapshot.child("attribute2").getValue().toString());
int attribute3=Integer.parseInt(dataSnapshot.child("attribute3").getValue().toString());
int attribute4=Integer.parseInt(dataSnapshot.child("attribute4").getValue().toString());
int attribute5=Integer.parseInt(dataSnapshot.child("attribute5").getValue().toString());
int attribute6=Integer.parseInt(dataSnapshot.child("attribute6").getValue().toString());
int attribute7=Integer.parseInt(dataSnapshot.child("attribute7").getValue().toString());
int attribute8=Integer.parseInt(dataSnapshot.child("attribute8").getValue().toString());
int attribute9=Integer.parseInt(dataSnapshot.child("attribute9").getValue().toString());
int attribute10=Integer.parseInt(dataSnapshot.child("attribute10").getValue().toString());
int attribute11=Integer.parseInt(dataSnapshot.child("attribute11").getValue().toString());
int attribute12=Integer.parseInt(dataSnapshot.child("attribute12").getValue().toString());
int attribute13=Integer.parseInt(dataSnapshot.child("attribute13").getValue().toString());
int attribute14=Integer.parseInt(dataSnapshot.child("attribute14").getValue().toString());
text1.setText(attribute1);
text2.setText(attribute2);
text3.setText(attribute3);
text4.setText(attribute4);
text5.setText(attribute5);
text6.setText(attribute6);
text7.setText(attribute7);
text8.setText(attribute8);
text9.setText(attribute9);
text10.setText(attribute10);
text11.setText(attribute11);
text12.setText(attribute12);
text13.setText(attribute13);
text14.setText(attribute14);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
}
}
我收到以下错误:-
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.appdoctor, PID: 4920
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.appdoctor.Main2Activity$1$1.onDataChange(Main2Activity.java:56)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Process 4920 terminated.
我尝试过的事情: -
- 添加了 google-services.json 文件
classpath 'com.google.gms:google-services:4.3.3'
在项目 Gradle 中添加apply plugin: 'com.google.gms.google-services'
在构建 Gradle 中添加- 改变了变量
attribute1
-attribute10
从String
到Integer
非常感谢任何帮助。谢谢!
解决方案
改变这个:
reff= FirebaseDatabase.getInstance().getReference().child("Attributes").child("M0pSXmPNPbXa3vgQeuc");
进入这个:
reff= FirebaseDatabase.getInstance().getReference().child("Attributes").child("-M0pSXmPNPbXa3vgQeuc");
该节点包含一个破折号child("-M0pSXmPNPbXa3vgQeuc");
推荐阅读
- python - 在 Cloud Datastore 中的字段上添加索引
- javascript - 使用滚动功能时减少闪烁/增加平滑度
- vb.net - VB上的自动触发网络摄像头QR扫描仪
- active-directory - 查找 AD/LDAP-User 的性别
- javascript - 如何在 React Native 中使用 AsyncStorage multiGet 检索数据
- angular - ng5 中的错误处理
- sql-server - SQL Server 意图锁是如何工作的?
- angular - 如何根据输入类型过滤项目列表中的数据,而不考虑使用 angular2 区分大小写
- python - 即使输入值不同,Django 也会从 MongoDB 给出相同的响应
- powerbi - Power BI - 删除字符串的前 7 位数字