Reputation: 159
I am trying to read the 'resName' and 'status' values from PjSxhQXCyXdSv07LxVzU7buvhkF2 under business_info node from firebase database, as seen .
I keep getting this error:
Attempt to invoke virtual method 'java.lang.String com.example.android.project_water.Utilities.RestaurantInformation.getResName()' on a null object reference.
Does anyone know how to solve this? Please let me know if more info is needed.
Code:
public class Info extends Fragment {
private DatabaseReference mDatabase;
private FirebaseAuth firebaseAuth;
private String businessID;
private TextView resNameChange, statusChange;
public Info() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_info, container, false);
SharedPreferences businessID1 = getActivity().getSharedPreferences("BUSINESS_ID", Context.MODE_PRIVATE);
businessID = businessID1.getString("businessID", "businessIDNotFound");
Log.i("BUSINESS KEY IS: ", businessID);
firebaseAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference();
resNameChange = rootView.findViewById(R.id.resNameChange);
statusChange = rootView.findViewById(R.id.statusChange);
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return rootView;
}
public void showData(DataSnapshot dataSnapshot) {
if (dataSnapshot.child("business_info").child(businessID).exists()) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
RestaurantInformation resInfo = new RestaurantInformation();
//error on the line below******
resInfo.setResName(ds.child(businessID).getValue(RestaurantInformation.class).getResName());
resInfo.setStatus(ds.child(businessID).getValue(RestaurantInformation.class).getStatus());
resNameChange.setText(resInfo.getResName());
statusChange.setText(resInfo.getStatus());
}
} else {
resNameChange.setText("NAME");
statusChange.setText("ONLINE");
}
}
}
My Model
public class RestaurantInformation {
private String resName;
private String status;
public RestaurantInformation() {
}
public RestaurantInformation(String resName) {
this.resName = resName;
}
public RestaurantInformation(String resName, String status) {
this.resName = resName;
this.status = status;
}
public String getResName() {
return resName;
}
public void setResName(String resName) {
this.resName = resName;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
Upvotes: 0
Views: 2268
Reputation: 599401
Your current code is downloading the entire database and then tries to figure out what to show client-side. That is a very wasteful use of your user's bandwidth. I recommend only listening for the business you're interested in:
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("business_info").child(businessID).addValueEventListener(new ValueEventListener() {
Then you can simplify your showData
method to:
public void showData(DataSnapshot snapshot) {
if (snapshot.exists()) {
RestaurantInformation resInfo = snapshot.getValue(RestaurantInformation.class)
resNameChange.setText(resInfo.getResName());
statusChange.setText(resInfo.getStatus());
} else {
resNameChange.setText("NAME");
statusChange.setText("ONLINE");
}
}
Upvotes: 1