gpt4 book ai didi

java - Firebase 数据库数据检索在空对象引用上给出 NullPointerException 'java.lang.String java.lang.Object.toString()'

转载 作者:行者123 更新时间:2023-12-02 08:57:47 25 4
gpt4 key购买 nike

我有一个 Firebase 数据库,将由两个 Android 项目使用。我正在尝试按照 YouTube 上的各种教程从一个项目/应用程序添加数据并在另一个项目/应用程序中检索数据。然而,我在检索数据时遇到了困难。这是我的数据库的快照。

enter image description here

我的应用加载 UI,但在运行以下代码并单击按钮获取数据后崩溃。

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 文件
  • 在项目中添加了类路径“com.google.gms:google-services:4.3.3” Gradle
  • 在构建中添加了应用插件:'com.google.gms.google-services' Gradle
  • 将变量 attribute1 - attribute10String 更改为整数

非常感谢任何帮助。谢谢!

最佳答案

更改此:

reff= FirebaseDatabase.getInstance().getReference().child("Attributes").child("M0pSXmPNPbXa3vgQeuc");

进入此:

reff= FirebaseDatabase.getInstance().getReference().child("Attributes").child("-M0pSXmPNPbXa3vgQeuc");

该节点包含破折号child("-M0pSXmPNPbXa3vgQeuc");

关于java - Firebase 数据库数据检索在空对象引用上给出 NullPointerException 'java.lang.String java.lang.Object.toString()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60381945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com