Home » Android » Android: butterKnife – @BindView – throw NPE

Android: butterKnife – @BindView – throw NPE

Posted by: admin November 1, 2017 Leave a comment

Questions:

Android Studio 2.3.3.

My layout xml file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/downloadProgressBar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

In my app/build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'

    android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.myproject"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        dev {
            initWith(debug)

        }
    }

}    

dependencies {
    annotationProcessor 'com.github.bumptech.glide:compiler:4.2.0'
    annotationProcessor "com.jakewharton:butterknife-compiler:$BUTTER_KNIFE_VERSION"   
    compile "com.jakewharton:butterknife:$BUTTER_KNIFE_VERSION" 
}

Here snippet of MyActivity:

public class MyActivityextends extends AppCompatActivity  {
    @BindView(R.id.downloadProgressBar) ProgressBar downloadProgressBar;       

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pdf_viewer_activity);
        ButterKnife.bind(this);
        ButterKnife.setDebug(true);    
        // THROW NPE!!!
        downloadProgressBar.setVisibility(View.GONE); 
     }    
}

But after start this activity I get NPE:

NullPointerException at com.myproject.MyActivity.onCreate(MyActivity.java:63)

Why? I think I correct initialize butterKnife lib. Project success build and run from Android Studio and from console.

Answers:

You are using Kotlin, so you should do

apply plugin: 'com.android.application'
//apply plugin: 'com.jakewharton.butterknife'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

And

dependencies {
    kapt 'com.github.bumptech.glide:compiler:4.2.0'
    kapt "com.jakewharton:butterknife-compiler:$BUTTER_KNIFE_VERSION"   
    compile "com.jakewharton:butterknife:$BUTTER_KNIFE_VERSION" 
}

Questions:
Answers:

Just need to declare the type of the variable downloadProgressBar.

@BindView(R.id.downloadProgressBar) ProgressBar downloadProgressBar;

Questions:
Answers:

Change

@BindView(R.id.downloadProgressBar) downloadProgressBar;

this line to

@BindView(R.id.downloadProgressBar) ProgressBar downloadProgressBar;