android – Possible reason for "Error:cannot generate view binders java.lang.NullPointerException"-ThrowExceptions

Exception or error:

I am using Android Studio for my Android projects. I faced an issue when builds crash with strange stacktrace, like this:

Error:Execution failed for task ':app:compileDevDebugJavaWithJavac'.
 java.lang.RuntimeException: failure, see logs for details.
  cannot generate view binders java.lang.NullPointerException
    at android.databinding.tool.reflection.ModelMethod.isBoxingConversion(ModelMethod.java:155)
    at android.databinding.tool.store.SetterStore.isBetterParameter(SetterStore.java:946)
    at android.databinding.tool.store.SetterStore.getBestSetter(SetterStore.java:838)

and it was seemed that the databinding became broken as whole.

I made refactoring before and moved classes between packages.

How to solve:

In my case, I relied on Android Studio when renaming and moving classes between packages. But it didn’t proceed correction for XMLs of layouts where were references on refactored classes in the type attribute of variable element in data.

So my previous type’s value pointed to non existing files and build crashed.

It’s simple mistake but it may take more time to find the source. Hope this would help someone.

###

For me, this started happening after updating Android Studio to version 3.5.2

To fix it, I downgraded Android Gradle Plugin

buildscript {

    repositories {
        //..........
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.1'
        //......
    }
}

###

Make sure you import all classes you reference in xml bindings

I had code something like:

android:visible="@{obj instanceof A}"

I was getting that same error.

Turns out that class A was not imported on top. Adding <import type="com.company.A"> tag resolved the problem.

P.S. android:visible is a custom binding adapter I have.

###

in my case my problem was in a LongClickListener that the method for this listener must have returned a boolean but my method returned void so when I added the correct return statement it worked just fine.

keep in mind if you use a data binding method you must return the correct value or else the error it throws is not helpful at all.

###

You may have stumbled upon this question after migrating to Android X. If that’s the case, and you’re positive your XML files are okay, but it’s still not working, you should start looking at libraries that generate code.

Libraries that generate code cannot be easily converted by the Jettifyer. Some info here – https://blog.danlew.net/2018/11/14/the-reality-of-migrating-to-androidx/

In my case, I updated the obvious ones, but I still was missing something. So, what I did was go into my develop branch (that wasn’t Android X), ran a build, and dug into the build folder to see all of the libraries that generated code. Once I did that, I was able to look at the suspects one by one until I found the dependency that was causing this issue. You could either update it, or, in my case, remove it, and this error was resolved. 🙂

###

The getter function for any instance that you refer from your xml file might not have been defined or the getter function might not have public access specifier.

###

I face this problem when I define variable like this (Android Studio don’t warning anything)

<data>
    <variable
        name="onGlobalLayoutA"
        type="ViewTreeObserver.OnGlobalLayoutListener"/>

    <import type="android.view.ViewTreeObserver"/>
</data>

And I solve it by

<data>
    <variable
        name="onGlobalLayoutA"
        type="android.view.ViewTreeObserver.OnGlobalLayoutListener"/>
</data>

Hope this would help someone.

###

In my case, the reason was that I used a type for a variable which is another module and that module is added as ‘implementation ‘ in build script. Changing to ‘api ‘ resolved the issue.

Clearly not the same issue as the one posted by atlascoder but I mention regardless, maybe someone gits this article with the same problem as I had.

###

As @hiddeneyes02 mentioned, this started happen when upgrading from Android Gradle Plugin from 3.5.1 to 3.5.2, this seems to be the bug:
https://issuetracker.google.com/issues/143778134

When I built the project with the command tool I got the same errors as in this post:
When building Android project with Android Gradle Plugin v. 3.5.2 my builds fail

###

Downgrading Android Gradle Plugin to 3.5.1 fixed the issue for me

Leave a Reply

Your email address will not be published. Required fields are marked *