question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KotlinNullPointerException

See original GitHub issue

Hi, thank’s for great lib. However, I faced the issue: crash when first open a fragment with calendar view. It happened once, and I think the problem is in using unsafe (!!) call.

kotlin.KotlinNullPointerException
        at com.kizitonwose.calendarview.ui.MonthViewHolder.bindMonth(MonthViewHolder.kt:39)
        at com.kizitonwose.calendarview.ui.CalendarAdapter.onBindViewHolder(CalendarAdapter.kt:144)
        at com.kizitonwose.calendarview.ui.CalendarAdapter.onBindViewHolder(CalendarAdapter.kt:27)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7033)
        at com.kizitonwose.calendarview.ui.CalendarAdapter.onBindViewHolder(CalendarAdapter.kt:135)
        at com.kizitonwose.calendarview.ui.CalendarAdapter.onBindViewHolder(CalendarAdapter.kt:27)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7075)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5991)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6258)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6097)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6093)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4115)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3832)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4385)
        at android.view.View.layout(View.java:20715)
        at android.view.ViewGroup.layout(ViewGroup.java:6202)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
        at android.view.View.layout(View.java:20715)
        at android.view.ViewGroup.layout(ViewGroup.java:6202)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20715)
        at android.view.ViewGroup.layout(ViewGroup.java:6202)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)


Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
EgorThe1stcommented, Aug 13, 2019

Here is the initialization:

 class DateSelectFragment : BaseFragment(), DateSelectView {

    companion object {
        fun newInstance(): DateSelectFragment = DateSelectFragment()
    }

    private val today =org.threeten.bp.LocalDate.now()

    private val dayBinder = getDayBinder()


    @InjectPresenter
    lateinit var presenter: DateSelectPresenter

    override fun getLayoutRes() = R.layout.fragment_date_select

    override fun initUI() {
        super.initUI()

    
        Handler().post { initCalendarView() }
    }

    private fun initCalendarView() {
        date_select_calendar_view.dayBinder = dayBinder

        date_select_calendar_view.monthFooterBinder = object : MonthHeaderFooterBinder<MonthFooterContainer> {
            override fun create(view: View) = MonthFooterContainer(view)

            override fun bind(container: MonthFooterContainer, month: CalendarMonth) {
            }
        }

        date_select_calendar_view.monthHeaderBinder = object : MonthHeaderFooterBinder<MonthHeaderContainer> {

            override fun create(view: View) = MonthHeaderContainer(view)

            override fun bind(container: MonthHeaderContainer, month: CalendarMonth) {
                container.monthTitle.text = month.yearMonth.format(DateTimeFormatter.ofPattern("MMM yyyy"))
            }
        }
        val currentMonth = YearMonth.now()
        val lastMonth = currentMonth.plusMonths(12)
        val firstDayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek

        date_select_calendar_view.setup(currentMonth, lastMonth, firstDayOfWeek)
    }

    class DayViewContainer(view: View) : ViewContainer(view) {
        val textView = view.calendar_day_text
    }

    class MonthHeaderContainer(view: View) : ViewContainer(view) {
        val monthTitle = view.calendar_month_title
    }

    class MonthFooterContainer(view: View) : ViewContainer(view)

    private fun getDayBinder(): DayBinder<DayViewContainer> {

        return object : DayBinder<DayViewContainer> {

            // Called only when a new container is needed.
            override fun create(view: View) = DayViewContainer(view)

            // Called every time we need to reuse a container.
            override fun bind(container: DayViewContainer, day: CalendarDay) {
                val dayText = container.textView

                fun setStandartDayColor() {
                    dayText.textColor = ContextCompat.getColor(requireContext(), R.color.blue_primary)
                }

                fun setInactiveDayColor() {
                    dayText.textColor = ContextCompat.getColor(requireContext(), R.color.calendar_inactive_date)
                }

                fun setFullSelectedDayView() {
                    dayText.textColor = ContextCompat.getColor(requireContext(), R.color.white)
                    dayText.background = resources.getDrawable(R.drawable.bg_calendar_selected_day_full, null)
                }

                fun clearTextBackground() {
                    dayText.background = null
                }

                fun setInitialState() {
                    dayText.text = null
                    setStandartDayColor()
                    clearTextBackground()
                }

                setInitialState()

                if (day.owner == DayOwner.THIS_MONTH) {
                    dayText.text = day.day.toString()

                    if (day.date.isBefore(today)) setInactiveDayColor()

                    if (!day.date.isBefore(today) && !day.date.isAfter(today)) {
                        setFullSelectedDayView()
                    }

                }

            }
        }
    }
}

Is it correct?

0reactions
EgorThe1stcommented, Aug 14, 2019

Is the issue fixed for you? Can I close this now?

Yes, I guess

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin - why do I get a KotlinNullPointerException
Starting from Kotlin 1.4, the exception will be NullPointerException instead (a different type), see here. So if any code catches explicitly ...
Read more >
KotlinNullPointerException - Kotlin Programming Language
Returns an array of stack trace elements representing the stack trace pertaining to this throwable. val Throwable.stackTrace: Array<StackTraceElement> ...
Read more >
build failed with kotlin.KotlinNullPointerException
Solved: I get this issue after an update followed by a rollback against the Gradle version. Background: Bamboo builds with Docker run tasks....
Read more >
java.lang.NullPointerException is thrown instead of kotlin ...
NullPointerException is thrown instead of kotlin.KotlinNullPointerException in not-null assertions. Duplicates 1. Duplicates 1 issue (0 unresolved).
Read more >
internal error (Caused by: kotlin.KotlinNullPointerException) #23
I got kotlin.KotlinNullPointerException when try to debug. The error seems to be caused by JDILauncher.createLaunchArgs The full error log: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found