You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击确认按钮触发空指针异常:调用Salon.getSalonId()时对象为空

Fixing the NullPointerException in Your Barber Booking App

Hey there, let's tackle that frustrating NullPointerException you're hitting when tapping the confirm button in your com.example.barberbooking app. The error clearly points to line 260 in BookingStep4Fragment.java, where you're trying to call salon.getSalonId() on a null Salon object.

What's Causing This?

This crash happens because the Salon instance you're trying to use hasn't been properly initialized or passed to BookingStep4Fragment. Before calling any methods on it, the object is null—so the app can't access a method on a non-existent instance, hence the crash.

Step-by-Step Troubleshooting & Fixes

  • First, pinpoint how you're getting the Salon object at line 260
    Look at where this Salon variable comes from. Common sources are:

    • Fragment arguments passed from a previous screen
    • A shared ViewModel tracking user selections
    • A static variable or singleton
  • Verify the Salon is being passed correctly (if using arguments)
    If you're sending the Salon via fragment arguments:

    1. Ensure your Salon class implements Parcelable (required to bundle objects for fragment navigation)
    2. Confirm you're adding the Salon to the arguments bundle when navigating to BookingStep4Fragment:
      Bundle args = new Bundle();
      args.putParcelable("selected_salon", userSelectedSalon);
      BookingStep4Fragment step4Fragment = new BookingStep4Fragment();
      step4Fragment.setArguments(args);
      
    3. Safely retrieve it in BookingStep4Fragment (add a null check here!):
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          // ... other code ...
          Salon salon = getArguments() != null ? getArguments().getParcelable("selected_salon") : null;
          if (salon == null) {
              // Handle the error gracefully—e.g., send user back to select a salon
              Toast.makeText(getContext(), "Please select a salon to continue!", Toast.LENGTH_SHORT).show();
              getParentFragmentManager().popBackStack();
              return inflater.inflate(R.layout.fragment_error, container, false);
          }
          // ... rest of your setup ...
      }
      
  • Add a guard clause before calling getSalonId()
    Even if you think the Salon should never be null, always add a null check to prevent crashes:

    // Replace your line 260 code with this:
    if (salon != null) {
        String salonId = salon.getSalonId();
        // Proceed with your booking submission logic
    } else {
        Log.e("BookingStep4", "Salon object is null—cannot complete booking");
        // Show user feedback to fix the issue
    }
    
  • Debug with log statements
    Add logs where you retrieve or assign the Salon object to trace where it becomes null:

    Log.d("BookingStep4", "Current Salon instance: " + (salon != null ? salon.getSalonName() : "null"));
    

    This will help you confirm if the Salon was never set, or if it got cleared somewhere before line 260.

Bonus Tip

If you're using a shared ViewModel to track the selected salon, double-check that the ViewModel is scoped correctly (e.g., using getActivityViewModel() if sharing across the entire booking flow) and that the Salon is assigned before navigating to BookingStep4Fragment.

内容的提问来源于stack exchange,提问作者ulses1994

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 14:02:38