3

I've written a custom view for displaying events in a 24 hour vertical scroll timeline.

In the onCreateView method in the fragment that inflates this view, I am inflating the view and then trying to set callbacks which are exposed by the view. However I am getting a null reference exception when trying to set a property on the custom view (i.e. the custom view is null).

Here is my layout (with my custom DayView inside the constraint layout):

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".application.home.HomeActivity">

        <com.condecosoftware.core.dayview.DayView
            android:id="@+id/dayView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:timeColumnWidth="52dp"
            app:timeColumnPadding="16dp"
            app:hourHeight="96dp"
            app:textSize="14sp"
            app:eventNameTextSize="18sp"
            app:hourSeparatorHeight="1dp"
            app:timeTextColor="@color/slateGrey"
            app:timeColumnBGColor="@color/white_2"
            app:hourSeparatorColor="@color/white_2"
            app:nowLineColor="@color/lipstick"
            app:eventColumnBGColor="@android:color/white"
            app:eventBGColor="@color/brightSkyBlue"
            app:eventCornerRadius="3.8dp"
            app:eventTextColor="@android:color/white"/>

    </android.support.constraint.ConstraintLayout>
</layout>

Now, I should note at this point, that the view IS inflating and being displayed correctly (if I remove the assignments to the callback in onCreateView).

Here is the code that is causing the NPE (bizzarly, Kotlin does NOT think that the variable I am trying to access can be null (I don't need to do safe cast or anything to get it to compile)):

@SuppressLint("MissingSuperCall")
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {

    val viewBinding = FragmentBookingsTodayBinding.inflate(inflater ?: LayoutInflater.from(context))

    // Setup day view listeners
    viewBinding.dayView.eventClickListener = this
    viewBinding.dayView.eventLongPressListener = this
    viewBinding.dayView.dayViewCallbacks = this

    this.viewBinding = viewBinding

    return viewBinding.root
}

I am aware I am supressing the super call on that method, but as you can see I am returning the viewbinding root at the bottom so it's ok.

Does anyone have any ideas?

EDIT (Stack Trace):

FATAL EXCEPTION: main
   Process: com.condecosoftware.roombooking.develop, PID: 3949
   java.lang.NullPointerException: Attempt to invoke virtual method 'void com.condecosoftware.core.dayview.DayView.setEventClickListener(com.condecosoftware.core.dayview.DayView$EventClickListener)' on a null object reference
       at com.condecosoftware.roombooking.application.home.FragmentBookingsToday.onCreateView(FragmentBookingsToday.kt:53)
       at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
       at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
       at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
       at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
       at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
       at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
       at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2199)
       at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651)
       at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:167)
       at android.support.v4.view.ViewPager.populate(ViewPager.java:1236)
       at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
       at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
       at android.view.View.measure(View.java:22002)
       at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1028)
       at android.view.View.measure(View.java:22002)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
       at android.view.View.measure(View.java:22002)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
       at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
       at android.view.View.measure(View.java:22002)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at android.view.View.measure(View.java:22002)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
       at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
       at android.view.View.measure(View.java:22002)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
       at com.android.internal.policy.DecorView.onMeasure(DecorView.java:721)
       at android.view.View.measure(View.java:22002)
       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2410)
       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1498)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
       at android.view.Choreographer.doCallbacks(Choreographer.java:723)
       at android.view.Choreographer.doFrame(Choreographer.java:658)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
       at android.os.Handler.handleCallback(Handler.java:789)
       at android.os.Handler.dispatchMessage(Handler.java:98)
       at android.os.Looper.loop(Looper.java:164)
10-21 14:05:30.514 3949-3949/com.condecosoftware.roombooking.develop E/AndroidRuntime:
       at android.app.ActivityThread.main(ActivityThread.java:6541)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
tynn
  • 38,113
  • 8
  • 108
  • 143
Thomas Cook
  • 4,371
  • 2
  • 25
  • 42
  • I might also add, that in my custom view code, I am initializing a load of properties in Kotlin's "init { }" block. – Thomas Cook Oct 21 '17 at 13:52
  • 1
    Always post you stacktrace and all relevant parts of your code please. – tynn Oct 21 '17 at 13:57
  • 1
    How the hell is this a duplicate of "what's a null pointer and how to fix it?" Are you effing kidding me? – Thomas Cook Oct 21 '17 at 14:15
  • Do you have multiple layout resources for different configurations? Also better use `inflater!!` instead of `inflater ?: LayoutInflater.from(context)` or define it as non-null `LayoutInflater`. – tynn Oct 21 '17 at 14:38
  • How about moving the listeners to onViewCreated instead? – fejd Oct 21 '17 at 23:09
  • @ThomasCook did you ever manage to solve this issue? We're facing the same problem. Using `onViewCreated` results in the same: `null`. The view is visible, but not accessible... – necavit Jun 22 '20 at 08:40
  • @necavit - I did, but I can't for the life of me remember how. – Thomas Cook Jun 22 '20 at 09:12
  • 1
    @ThomasCook hehehe! No problem, absolutely understandable ;) It's a weird issue indeed! We're now trying to move the view usage to onActivityCreated to see if it helps... Fingers crossed! Cheers! – necavit Jun 23 '20 at 22:01
  • @necavit - hey, I think I remember something now; try using `doOnLayout { // access view }` to make sure the view is fully setup before accessing custom properties on it (in my case, it was properties on a custom view called "DayView". – Thomas Cook Jun 24 '20 at 07:58

0 Answers0