我正在尝试从我的Firebase检索2个数据分支,但是我遇到了nullpointException,我不知道为什么
这是数据库
用于检索烹调时间和类别的代码
FirebaseDatabase.getInstance().getReference().child( "recipes" )
.addListenerForSingleValueEvent( new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
/*Recipe recipe = snapshot.getValue( Recipe.class );*/
DatabaseReference recipes = FirebaseDatabase.getInstance().getReference("recipes");
Recipe recipe_test = snapshot.child( "recipe_1" ).getValue(Recipe.class);
category.setText( recipe_test.getCategory() );
cookinTime.setText( recipe_test.getCookingtime() );
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
} );
}
食谱类
package com.example.myapplicationdatatest;
import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;
public class Recipe {
private String category;
private long cookingtime;
private String description;
private Map<String, Object> amountOfIngredients;
private String nameOfRecipe;
private Map<String, Object> Ingredients;
public Recipe() {};
public Recipe(String category, long cookingtime, String description,Map<String, Object> amountOfIngredients, String nameOfRecipe, Map<String, Object> Ingridients) {
this.category=category;
this.cookingtime=cookingtime;
this.description=description;
this.amountOfIngredients=amountOfIngredients;
this.nameOfRecipe=nameOfRecipe;
this.Ingredients=Ingridients;
}
public String getCategory() {
return category;
}
public String getCookingtime() {
long ct = cookingtime;
String result = String.valueOf( ct );
return result;
}
public String getDescription() {
return description;
}
public Map<String, Object> getAmountOfIngredients() {
return amountOfIngredients;
}
public String getNameOfRecipe() {
return nameOfRecipe;
}
public Map<String, Object> getIngredients() {
return Ingredients;
}
}
和例外
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplicationdatatest, PID: 20846
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.myapplicationdatatest.Recipe.getCategory()' on a null object reference
at com.example.myapplicationdatatest.MainActivity$1.onDataChange(MainActivity.java:73)
at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@19.1.0:179)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.1.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.1.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.1.0: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)
I/Process: Sending signal. PID: 20846 SIG: 9
Disconnected from the target VM, address: 'localhost:8611', transport: 'socket'
我为数据库创建了一个有效的食谱类,因为我在数据库中只有一个食谱之前就尝试了
thx为您提供帮助
答案 0 :(得分:2)
您正在听recipes
,然后循环播放其下的孩子。这意味着在第一次迭代中,snapshot
已经指向recipe_1
,并且您不再需要为该子地址子地址了:
FirebaseDatabase.getInstance().getReference().child( "recipes" )
.addListenerForSingleValueEvent( new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Recipe recipe_test = snapshot.getValue( Recipe.class );
如果您想显式地获取某些食谱,请摆脱循环并使用直接child("...")
访问权限:
FirebaseDatabase.getInstance().getReference().child( "recipes" )
.addListenerForSingleValueEvent( new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Recipe recipe1 = dataSnapshot.child("recipe_1").getValue( Recipe.class );
Recipe recipe2 = dataSnapshot.child("recipe_2").getValue( Recipe.class );
答案 1 :(得分:2)
使用.getValue()
时,请注意:
此方法用于编组此快照中包含的数据 进入您选择的课程。该课程必须适合2个简单 约束:
- 该类必须具有不带参数的默认构造函数
- 该类必须为要分配的属性定义公共获取方法。没有公共获取者的属性将设置为 实例反序列化时的默认值
我建议在定义类变量之后,只需在Android Studio中执行Alt+Insert
即可生成公共获取方法。
此外,正如弗兰克在回答中指出的那样,通过执行以下操作,您将收到“食谱”下的所有食谱:
FirebaseDatabase.getInstance().getReference().child( "recipes" )
.addListenerForSingleValueEvent( new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Recipe recipe_test = snapshot.getValue( Recipe.class );
}
});
现在在for-each循环中,您需要使用这些值。例如,当您想让快照具有key =“ recipe_2”时,您需要这样做:
if( snapshot.getKey().equals("recipe_2")) {
//do your stuff
}
完整代码:
FirebaseDatabase.getInstance().getReference().child( "recipes" )
.addListenerForSingleValueEvent( new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
if( snapshot.getKey().equals("recipe_2")) {
//do your stuff
Recipe recipe_2 = snapshot.getValue( Recipe.class );
}
}
});