From b69d1c8ce9d615dd88bcd6b7171475709504fdc7 Mon Sep 17 00:00:00 2001 From: Alex <78383757+Lextical@users.noreply.github.com> Date: Fri, 10 Apr 2026 19:49:54 -0600 Subject: [PATCH] Added null checks for Appointment and Adoptions to make sure the spinner can load --- .../detailfragments/AdoptionDetailFragment.java | 13 ++++++++++--- .../detailfragments/AppointmentDetailFragment.java | 7 ++++++- .../viewmodels/AdoptionDetailViewModel.java | 8 ++++---- .../viewmodels/AppointmentDetailViewModel.java | 12 ++++++------ 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/android/app/src/main/java/com/example/petstoremobile/fragments/listfragments/detailfragments/AdoptionDetailFragment.java b/android/app/src/main/java/com/example/petstoremobile/fragments/listfragments/detailfragments/AdoptionDetailFragment.java index 1e109213..c14ae13a 100644 --- a/android/app/src/main/java/com/example/petstoremobile/fragments/listfragments/detailfragments/AdoptionDetailFragment.java +++ b/android/app/src/main/java/com/example/petstoremobile/fragments/listfragments/detailfragments/AdoptionDetailFragment.java @@ -225,9 +225,16 @@ public class AdoptionDetailFragment extends Fragment { try { fee = new BigDecimal(feeStr); } catch (NumberFormatException ignored) {} } - DropdownDTO customer = viewModel.getCustomerList().getValue().get(binding.spinnerAdoptionCustomer.getSelectedItemPosition() - 1); - DropdownDTO pet = viewModel.getPetList().getValue().get(binding.spinnerAdoptionPet.getSelectedItemPosition() - 1); - DropdownDTO store = viewModel.getStoreList().getValue().get(binding.spinnerAdoptionStore.getSelectedItemPosition() - 1); + List customerListVal = viewModel.getCustomerList().getValue(); + List petListVal = viewModel.getPetList().getValue(); + List storeListVal = viewModel.getStoreList().getValue(); + if (customerListVal == null || petListVal == null || storeListVal == null) { + Toast.makeText(getContext(), "Data not loaded yet, please try again", Toast.LENGTH_SHORT).show(); + return; + } + DropdownDTO customer = customerListVal.get(binding.spinnerAdoptionCustomer.getSelectedItemPosition() - 1); + DropdownDTO pet = petListVal.get(binding.spinnerAdoptionPet.getSelectedItemPosition() - 1); + DropdownDTO store = storeListVal.get(binding.spinnerAdoptionStore.getSelectedItemPosition() - 1); Long employeeId = null; if (binding.spinnerAdoptionEmployee.getSelectedItemPosition() > 0 && viewModel.getEmployeeList().getValue() != null) { diff --git a/android/app/src/main/java/com/example/petstoremobile/fragments/listfragments/detailfragments/AppointmentDetailFragment.java b/android/app/src/main/java/com/example/petstoremobile/fragments/listfragments/detailfragments/AppointmentDetailFragment.java index 6c524d75..e08f7fdc 100644 --- a/android/app/src/main/java/com/example/petstoremobile/fragments/listfragments/detailfragments/AppointmentDetailFragment.java +++ b/android/app/src/main/java/com/example/petstoremobile/fragments/listfragments/detailfragments/AppointmentDetailFragment.java @@ -248,7 +248,12 @@ public class AppointmentDetailFragment extends Fragment { String date = binding.etAppointmentDate.getText().toString().trim(); String time = buildTimeString(); - String status = binding.spinnerAppointmentStatus.getSelectedItem().toString().toUpperCase(); + Object selectedStatus = binding.spinnerAppointmentStatus.getSelectedItem(); + if (selectedStatus == null) { + Toast.makeText(getContext(), "Please select a status", Toast.LENGTH_SHORT).show(); + return; + } + String status = selectedStatus.toString().toUpperCase(); if (!viewModel.isValidFutureBooking(status, date, time)) { DialogUtils.showInfoDialog(requireContext(), "Invalid Time", "Booked appointments must be in the future."); diff --git a/android/app/src/main/java/com/example/petstoremobile/viewmodels/AdoptionDetailViewModel.java b/android/app/src/main/java/com/example/petstoremobile/viewmodels/AdoptionDetailViewModel.java index c15766f9..aa4a78cb 100644 --- a/android/app/src/main/java/com/example/petstoremobile/viewmodels/AdoptionDetailViewModel.java +++ b/android/app/src/main/java/com/example/petstoremobile/viewmodels/AdoptionDetailViewModel.java @@ -248,10 +248,10 @@ public class AdoptionDetailViewModel extends ViewModel { String adoptionDate = a.getAdoptionDate() != null ? a.getAdoptionDate() : ""; updateViewState(state -> { - state.selectedPetId = a.getPetId() != null ? a.getPetId() : -1; - state.selectedCustomerId = a.getCustomerId() != null ? a.getCustomerId() : -1; - state.selectedStoreId = a.getSourceStoreId() != null ? a.getSourceStoreId() : -1; - state.selectedEmployeeId = a.getEmployeeId() != null ? a.getEmployeeId() : -1; + state.selectedPetId = a.getPetId(); + state.selectedCustomerId = a.getCustomerId(); + state.selectedStoreId = a.getSourceStoreId(); + state.selectedEmployeeId = a.getEmployeeId(); state.adoptionDate = adoptionDate; state.adoptionFee = a.getAdoptionFee() != null ? a.getAdoptionFee().toPlainString() : ""; state.selectedStatus = formattedStatus; diff --git a/android/app/src/main/java/com/example/petstoremobile/viewmodels/AppointmentDetailViewModel.java b/android/app/src/main/java/com/example/petstoremobile/viewmodels/AppointmentDetailViewModel.java index 18c20e24..fae681d9 100644 --- a/android/app/src/main/java/com/example/petstoremobile/viewmodels/AppointmentDetailViewModel.java +++ b/android/app/src/main/java/com/example/petstoremobile/viewmodels/AppointmentDetailViewModel.java @@ -72,13 +72,13 @@ public class AppointmentDetailViewModel extends ViewModel { */ public void loadInitialFormData() { customerRepository.getCustomerDropdowns().observeForever(r -> { - if (r.status == Resource.Status.SUCCESS) customers.setValue(r.data); + if (r != null && r.status == Resource.Status.SUCCESS) customers.setValue(r.data); }); storeRepository.getStoreDropdowns().observeForever(r -> { - if (r.status == Resource.Status.SUCCESS) stores.setValue(r.data); + if (r != null && r.status == Resource.Status.SUCCESS) stores.setValue(r.data); }); serviceRepository.getAllServices(0, 200, null, "serviceName").observeForever(r -> { - if (r.status == Resource.Status.SUCCESS && r.data != null) services.setValue(r.data.getContent()); + if (r != null && r.status == Resource.Status.SUCCESS && r.data != null) services.setValue(r.data.getContent()); }); } @@ -207,7 +207,7 @@ public class AppointmentDetailViewModel extends ViewModel { */ private void loadPetsForCustomer(Long customerId) { petRepository.getCustomerPets(customerId).observeForever(r -> { - if (r.status == Resource.Status.SUCCESS) customerPets.setValue(r.data); + if (r != null && r.status == Resource.Status.SUCCESS) customerPets.setValue(r.data); }); } @@ -216,7 +216,7 @@ public class AppointmentDetailViewModel extends ViewModel { */ private void loadEmployeesForStore(Long storeId) { storeRepository.getStoreEmployees(storeId).observeForever(r -> { - if (r.status == Resource.Status.SUCCESS) storeEmployees.setValue(r.data); + if (r != null && r.status == Resource.Status.SUCCESS) storeEmployees.setValue(r.data); }); } @@ -228,7 +228,7 @@ public class AppointmentDetailViewModel extends ViewModel { public LiveData> loadAppointment() { MutableLiveData> result = new MutableLiveData<>(); repository.getAppointmentById(appointmentId).observeForever(resource -> { - if (resource.status == Resource.Status.SUCCESS && resource.data != null) { + if (resource != null && resource.status == Resource.Status.SUCCESS && resource.data != null) { AppointmentDTO a = resource.data; isOriginallyCancel = "CANCELLED".equalsIgnoreCase(a.getAppointmentStatus()); currentCustomerId = a.getCustomerId();