Q:

Android App Crashes on Deleting child node From Firebase Android in Image Slider

Here, I have successfully added a Realtime Firebase Database Image Slider which Retrieves images and shows them in an image slider. Everything's working fine. All updation of images and all. Except when I'm deleting the child node in my Firebase realtime database the app crashes. 
Home Fragment
public class HomeFragment extends Fragment {

DatabaseReference databaseReference;
List<ImageSliderModel> slideLists;

SliderView sliderView;
int TotalCounts;

public HomeFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);

    databaseReference = FirebaseDatabase.getInstance().getReference();
    slideLists = new ArrayList<>();

    sliderView = view.findViewById(R.id.imageSlider);

    FirebaseDatabase.getInstance().getReference("ImagesLinks").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot != null) {
                Long counts = dataSnapshot.getChildrenCount();
                TotalCounts = counts.intValue();

                sliderView.setSliderAdapter(new ImageSliderAdapter(getActivity(), TotalCounts));
                sliderView.setIndicatorAnimation(IndicatorAnimations.WORM);
                sliderView.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
                sliderView.setAutoCycleDirection(SliderView.AUTO_CYCLE_DIRECTION_BACK_AND_FORTH);
                sliderView.setIndicatorSelectedColor(Color.WHITE);
                sliderView.setIndicatorUnselectedColor(Color.LTGRAY);
                sliderView.setScrollTimeInSec(3);
                sliderView.startAutoCycle();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
     return view;
   }
}

XML CODE
    <ViewFlipper
        android:id="@+id/viewflipper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoStart="true"
        android:flipInterval="2000">

        <com.smarteist.autoimageslider.SliderView
            android:id="@+id/imageSlider"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            app:sliderAutoCycleDirection="back_and_forth"
            app:sliderIndicatorAnimationDuration="600"
            app:sliderIndicatorGravity="center_horizontal|bottom"
            app:sliderIndicatorSelectedColor="#FFFFFFFF"
            app:sliderIndicatorUnselectedColor="#FFCCCCCC"
            app:sliderScrollTimeInSec="3"
            app:sliderStartAutoCycle="true" />
    </ViewFlipper>

Image Slider Adapter
public class ImageSliderAdapter extends SliderViewAdapter<SliderviewHolder> {

Context context;
int setTotalCount;
String ImageLink;

public ImageSliderAdapter(Context context, int setTotalCount) {
    this.setTotalCount = setTotalCount;
    this.context = context;
}

@Override
public SliderviewHolder onCreateViewHolder(ViewGroup parent) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.slider_item_layout,parent,false);
    return new SliderviewHolder(view) ;
}

@Override
public void onBindViewHolder(final SliderviewHolder viewHolder, final int position) {
    FirebaseDatabase.getInstance().getReference("ImagesLinks").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            switch (position){
                case 0:
                    ImageLink = dataSnapshot.child("1").getValue().toString();
                    Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
                    break;

                case 1:
                    ImageLink = dataSnapshot.child("2").getValue().toString();
                    Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
                    break;

                case 2:
                    ImageLink = dataSnapshot.child("3").getValue().toString();
                    Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
                    break;

                case 3:
                    ImageLink = dataSnapshot.child("4").getValue().toString();
                    Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
                    break;

                case 4:
                    ImageLink = dataSnapshot.child("5").getValue().toString();
                    Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
                    break;

                case 5:
                    ImageLink = dataSnapshot.child("6").getValue().toString();
                    Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
                    break;

                case 6:
                    ImageLink = dataSnapshot.child("7").getValue().toString();
                    Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
                    break;

                case 7:
                    ImageLink = dataSnapshot.child("8").getValue().toString();
                    Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
                    break;

                case 8:
                    ImageLink = dataSnapshot.child("9").getValue().toString();
                    Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
                    break;

                case 9:
                    ImageLink = dataSnapshot.child("10").getValue().toString();
                    Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
                    break;

                default:
                    return;
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
  }

@Override
public int getCount() {
    return setTotalCount;
   }
}

class SliderviewHolder extends SliderViewAdapter.ViewHolder {
ImageView sliderImageview;
View itemView;
public SliderviewHolder(View itemView) {
    super(itemView);
    this.itemView = itemView;
    sliderImageview = itemView.findViewById(R.id.imageview02);
  }
}

Image Slider Model
public class ImageSliderModel {

String imageUrl;

public ImageSliderModel() {
}

public ImageSliderModel(String imageUrl){
    this.imageUrl = imageUrl;
}

public String getImageUrl() {
    return imageUrl;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
  }
 }

This is all with the source code. Note: All values in the firebase root child are updating correctly, except child from root node deleting. 
here's Crash Log Report 
enter code here
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.demo.test, PID: 30101
java.lang.NullPointerException: Attempt to invoke virtual method 
'java.lang.String java.lang.Object.toString()' on a null object reference         
at com.demo.test.HomePage.ImageSliderAdapter$1.onDataChange(ImageSliderAdapter.java:50)
 at com.google.android.gms.internal.zzegf.zza(Unknown Source:13)
 at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)
 at com.google.android.gms.internal.zzeig.run(Unknown Source:71)
 at android.os.Handler.handleCallback(Handler.java:790)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:164)
 at android.app.ActivityThread.main(ActivityThread.java:6626)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
 Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'

Firebase Model

A:

In the ImageSliderAdapter.java file, when you call dataSnapshot.child("X").getValue(), since "X" is hardcoded, it might return null after you have deleted the child node. So, for this to work, please update ImageSliderAdapter.java as follows:
ImageSliderAdapter.java
public class ImageSliderAdapter extends SliderViewAdapter < SliderviewHolder > {

 Context context;
 List<String> slideLists;

 public ImageSliderAdapter(Context context, List<String> slideLists) {
  this.slideLists = slideLists;
  this.context = context;
 }

 @Override
 public SliderviewHolder onCreateViewHolder(ViewGroup parent) {
  View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.slider_item_layout, parent, false);
  return new SliderviewHolder(view);
 }

 @Override
 public void onBindViewHolder(final SliderviewHolder viewHolder, final int position) {
    String imageUrl = slideLists.get(position); 
  Glide.with(viewHolder.itemView).load(imageUrl).into(viewHolder.sliderImageview);
 }

 @Override
 public int getCount() {
  return slideLists.size();
 }
}

class SliderviewHolder extends SliderViewAdapter.ViewHolder {
 ImageView sliderImageview;
 View itemView;

 public SliderviewHolder(View itemView) {
  super(itemView);
  this.itemView = itemView;
  sliderImageview = itemView.findViewById(R.id.imageview02);
 }
}

And update the HomeFragment.java as follows. I am converting the child node data to string and passing it as a list to the adapter. Because calling addValueEventListener() inside onBindViewHolder() is not a good idea since it will create multiple listeners which is not required in this context.
HomeFragment.java
public class HomeFragment extends Fragment {

DatabaseReference databaseReference;
List<String> slideLists;

SliderView sliderView;
int TotalCounts;

public HomeFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);

    databaseReference = FirebaseDatabase.getInstance().getReference();
    slideLists = new ArrayList<>();

    sliderView = view.findViewById(R.id.imageSlider);

    FirebaseDatabase.getInstance().getReference("ImagesLinks").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                slideLists.clear();

                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    String model = snapshot.getValue().toString();
                    slideLists.add(model);
                }
                sliderView.setSliderAdapter(new ImageSliderAdapter(getActivity(), slideLists));
                sliderView.setIndicatorAnimation(IndicatorAnimations.WORM);
                sliderView.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
                sliderView.setAutoCycleDirection(SliderView.AUTO_CYCLE_DIRECTION_BACK_AND_FORTH);
                sliderView.setIndicatorSelectedColor(Color.WHITE);
                sliderView.setIndicatorUnselectedColor(Color.LTGRAY);
                sliderView.setScrollTimeInSec(3);
                sliderView.startAutoCycle();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
     return view;
   }
}

