Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible reason for "Error:cannot generate view binders java.lang.NullPointerException"

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.

like image 490
atlascoder Avatar asked Feb 04 '17 23:02

atlascoder


4 Answers

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.

like image 181
atlascoder Avatar answered Oct 22 '22 05:10

atlascoder


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'
        //......
    }
}
like image 35
hiddeneyes02 Avatar answered Oct 22 '22 07:10

hiddeneyes02


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.

like image 9
Daniels Šatcs Avatar answered Oct 22 '22 06:10

Daniels Šatcs


If this question is still relevant. For me I figured that in my xml layout file I was including a class (not an instance of one) as a variable, i.e I had this code

<variable
      name="Converters"
      type="com.example.flexapp.utils.Converters" />

and it should have been

<import type="com.example.flexapp.utils.Converters"/>

since this was a class and not an object.

like image 8
Steven Avatar answered Oct 22 '22 05:10

Steven