androidnullpointerexceptioninitializationcalendarviewcannot-find-symbol

Why CalendarView in android show error when declare or initialize?


good day, I meet some error when I try to code this.

If I initial when declare, it show like this .

public class FragmentCalendar extends Fragment {
    Calendar calendar;
    CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView);
    Date date;
}

the error shown as below:

error: cannot find symbol CalendarView calendarView = (CalendarView)findViewById(R.id.calendarView); ^ symbol: method findViewById(int) location: class FragmentCalendar

If I initial after declare, it show like this.

public class FragmentCalendar extends Fragment {
    Calendar calendar;
    CalendarView calendarView;
    Date date;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState){
        calendar = calendar.getInstance(); 
        date = calendar.getTime();
        System.out.println("current date : "+date);

        calendarView.findViewById(R.id.calendarView);
        View view = inflater
                .inflate(R.layout.fragment_calendar, container, false); 
        return view;
    }

then show the error as below:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.CalendarView.findViewById(int)' on a null object reference at com.example.doodoobii.FragmentCalendar.onCreateView(FragmentCalendar.java:50)

Is already "import android.widget.CalendarView;" and "import java.util.Calendar;".

I can't get what is the cause, and no idea how to fix this. Please teach me , thanks for all senior here.

This is the xml file of it.

<CalendarView
            android:id="@+id/calendarView"
            android:layout_height="100dp"
            android:layout_width="wrap_content"
            android:theme="@style/CalendarViewCustom"
            android:dateTextAppearance="@style/CalendarDateCustomText"
            android:weekDayTextAppearance="@style/CalendarWeekCustomText"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

I wish it can work nicely. I wish to know why it will error, what I did wrong to it? Thank you.


Solution

  • calendarView is initialized by finding the CalendarView within the inflated view using view.findViewById(). This should resolve the NullPointerException that you were encountering.

    public class FragmentCalendar extends Fragment {
        Calendar calendar;
        CalendarView calendarView;
        Date date;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState){
            View view = inflater.inflate(R.layout.fragment_calendar, container, 
                               false);
            calendarView = view.findViewById(R.id.calendarView); // Initialize calendarView here
            calendar = Calendar.getInstance(); 
            date = calendar.getTime();
            System.out.println("current date : "+date);
            return view;
        }
    }