【发布时间】:2019-09-13 14:26:16
【问题描述】:
我试图从我的 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'
我为数据库创建了一个有效的配方类,因为我之前尝试过,当时数据库中只有一个配方
感谢您的帮助
【问题讨论】:
-
您的
recipe_test对象是null。向我们展示您的Recipe课程的内容。 -
Doesnt
getValue()需要类字段的公共设置器吗?似乎 SDK 找不到该字段的设置器,这就是为什么总是有空值的原因。您在控制台中是否有任何来自 Firebase SDK 的日志?
标签: android firebase firebase-realtime-database