Reputation: 876
I'm trying to use removeValue() to remove items from my database. when i do my app crashes because there obviously becomes a null pointer in the onChange method.
What is the best way to remove Values from the database to stop my app from crashing?
these are the values I'm removing
DatabaseReference detailref = bookingref.child("Bookings").child(customerid);
detailref.child("Housenumber").removeValue();
detailref.child("Address").removeValue();
detailref.child("Postcode").removeValue();
detailref.child("Phone").removeValue();
detailref.child("Date").removeValue();
and in another activity, i call them using a value event Listener.
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.getChildrenCount()!=0){
house = dataSnapshot.child("Housenumber").getValue().toString();
address = dataSnapshot.child("Address").getValue().toString();
postcode = dataSnapshot.child("Postcode").getValue().toString();
phone = dataSnapshot.child("Phone").getValue().toString();
date = dataSnapshot.child("Date").getValue().toString();
here is the null pointer error from my Logcat
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at studios.p9p.h20.showroomfinish_adminapp.Booking_details$4.onDataChange
Upvotes: 0
Views: 243
Reputation: 876
i used the answer above but in a much simpler way. i created a firebase model with all my strings in it called bookingModel() then in side my ondataChange() i could change all the bulky code below :)
house = dataSnapshot.child("Housenumber").getValue().toString();
address = dataSnapshot.child("Address").getValue().toString();
postcode = dataSnapshot.child("Postcode").getValue().toString();
phone = dataSnapshot.child("Phone").getValue().toString();
date = dataSnapshot.child("Date").getValue().toString();
id already got a userid string for the ref node called customerId so i used this instead in my onDataChanged()
if(dataSnapshot.hasChild(customerId))
bookingModel firebase_model =
dataSnapshot.child(customerId).getValue(bookingModel.class)
then to delete i call
detailRef.child(customerId).removeValue();
Upvotes: 1
Reputation: 85
Could you please put more details of what are you trying to do? I think it's crashing because you're removing values separately. If you're trying to remove all the child data, you could try something like
DatabaseReference detailref = bookingref.child("Bookings").child(customerid);
detailref.removeValue();
If not, maybe you could check if it exist before you try to get the values... Not the best or beautiful way to to it, but it may work...
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()
{
if (dataSnapshot.getChildrenCount()!=0){
if(dataSnapshot.hasChild("Housenumber")
{
house = dataSnapshot.child("Housenumber").getValue().toString();
}
if(dataSnapshot.hasChild("Address")
{
address = dataSnapshot.child("Address").getValue().toString();
}
if(dataSnapshot.hasChild("Postcode")
{
postcode = dataSnapshot.child("Postcode").getValue().toString();
}
if(dataSnapshot.hasChild("Phone")
{
phone = dataSnapshot.child("Phone").getValue().toString();
}
if(dataSnapshot.hasChild("Date")
{
date = dataSnapshot.child("Date").getValue().toString();
}
} }
Hope this helps :)
Upvotes: 2