我是android编程的新手,我正在从firebase数据库中检索数据到recyclerview但是我得到空指针异常。错误如下。
E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList com.example.priyanka.rent_ol.Firebase.Firebase_helper.retrieve(java.lang.String)' on a null object reference
at com.example.priyanka.rent_ol.Get.getData(Get.java:39)
at com.example.priyanka.rent_ol.AllProducts.onClick(AllProducts.java:43)
at android.view.View.performClick(View.java:4757)
at android.view.View$PerformClick.run(View.java:19757)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5235)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
allproducts类中的Onclick方法如下
@覆盖 public void onClick(查看视图){
if(view.getId()==R.id.electronics_button){
Toast.makeText(AllProducts.this,"electronics clicked",Toast.LENGTH_LONG).show();
category="Cloths";
getitem.getData(category);
startActivity(new Intent(getApplicationContext(),Get.class));
}
}
Get Data方法如下
public void getData(String category) {
//SETUP RV
// rv= (RecyclerView) findViewById(R.id.recyclerv);
// rv.setLayoutManager(new LinearLayoutManager(this));
catS=category;
adapter = new MyAdapter(this, helper.retrieve(catS));
rv.setAdapter(adapter);
}
我的Firebase助手类如下
DatabaseReference AdvertisementRef;
ArrayList<SaveAddInformation>adinfolist=new ArrayList();
MyAdapter adapter;
public Firebase_helper(DatabaseReference advertisementRef) {
AdvertisementRef = advertisementRef;
}
//READ DATA
public ArrayList<SaveAddInformation> retrieve(String cat){
String categoryS;
categoryS=cat;
AdvertisementRef.orderByChild("main_category").equalTo(categoryS).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {
SaveAddInformation mModel = eventSnapshot.getValue(SaveAddInformation.class);
Log.d("DATA" ,""+ mModel);
adinfolist.add(mModel);
}
// adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return adinfolist;
}