当我尝试运行应用程序时,当我清除应用程序的缓存时,我收到以下错误消息:它工作正常,但是当我关闭它并再次运行它时,它崩溃了
Attempt to invoke virtual method 'java.lang.String com.ggsbusiness.amirmemon.comsatsbusservice.Model.ComsatsDriver.getCarType()' on a null object reference
当尝试从Firebasedatabse获取当前的汽车类型时。
drivers = FirebaseDatabase.getInstance().getReference(Common.driver_tbl).child(Common.currentBusDriver.getCarType());
来自Common.class
public class Common {
public static final String driver_tbl = "Drivers";
public static final String user_driver_tbl = "DriversInformation";
public static final String user_rider_tbl = "RidersInformation";
public static final String pickup_request_tbl = "PickupRequest";
public static final String baseURL = "https://maps.googleapis.com";
public static ComsatsDriver currentBusDriver;
public static IGoogleAPI getGoogleAPI()
{
return RetrofitClient.getClient(baseURL).create(IGoogleAPI.class);
}
}
这是ComsatsDriver.class:
public class ComsatsDriver {
private String name,phone,carType;
public ComsatsDriver(){
}
public ComsatsDriver(String name, String phone, String carType) {
this.name = name;
this.phone = phone;
this.carType = carType;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCarType() {
return carType;
}
public void setCarType(String carType) {
this.carType = carType;
}
}
答案 0 :(得分:0)
这里:
child(Common.currentBusDriver.getCarType());
您实际上正在从Firebase中收到getCarType()
的空对象,这就是为什么它显示空错误的原因。
相反,获得如下节点:
drivers = FirebaseDatabase.getInstance().getReference(Common.driver_tbl).child("CurrentBusDriverNode"); // Or whatever your json output node is
然后遍历onDataChange()
中侦听器的输出,然后就可以获取数据。
检查这段代码Firebase sample。然后,the Listener。