19

I want to using Butter Knife in my project.I did everything according to the Butter Knife tutorial. But when I set anything to the views (setText, setClickListener ...) I got null object reference exception.

This is my code:

public class LoginActivity extends AppCompatActivity implements LoginView, View.OnClickListener {

@BindView(R.id.acEtUsername) AppCompatEditText userName;
@BindView(R.id.acEtPassword) AppCompatEditText password;
@BindView(R.id.prgCheckLogin) ProgressBar prgCheckLogin;
@BindView(R.id.btnLogin) Button btnLogin;

LoginPresenter loginPresenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    ButterKnife.bind(this);
    ButterKnife.setDebug(true);

    loginPresenter = new LoginPresenterImpl(this);

    btnLogin.setOnClickListener(this); // or userName.setText("userName");
  }
  /** Other Methods **/

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/acEtUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_marginRight="32dp"
        android:layout_marginLeft="32dp"
        android:hint="@string/user_name"/>

    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/acEtPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_marginTop="8dp"
        android:layout_marginRight="32dp"
        android:layout_marginLeft="32dp"
        android:hint="@string/password"/>

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="8dp"
        android:text="@string/login"/>

    <ProgressBar
        android:id="@+id/prgCheckLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:layout_gravity="center|bottom"/>

</LinearLayout>

And error log

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.AppCompatEditText.setText(java.lang.CharSequence)' on a null object reference

What's wrong in my code ?

Thanks

FarshidABZ
  • 3,860
  • 4
  • 32
  • 63

6 Answers6

43

I fixed it.

There is a problem with my build.gradle

I forgot to add

apt 'com.jakewharton:butterknife-compiler:8.0.1'

to the build.gradle

Thank everyone

UPDATE

If you are using neenbedankt.android-apt plugin first remove it.

Then remove apt 'com.jakewharton:butterknife-compiler:8.0.1'

And then add annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' to the dependencies.

UPDATE 2

If you are using kotlin replace :

annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

with:

kapt 'com.jakewharton:butterknife-compiler:8.8.1'

And don't forget to add

apply plugin: 'kotlin-kapt'

after:

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
FarshidABZ
  • 3,860
  • 4
  • 32
  • 63
17

In your onCreate method, make sure you have the line:

ButterKnife.bind(this);

Without that line, the binds you set up aren't performed, and the views remain null.

KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
Jerry Frost
  • 467
  • 4
  • 6
12

Use the following if your are using the new Butter Knife version:

compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

If you are using Kotlin, replace annotationProcessor with kapt.

UPDATE:

If you are using Gradle plugin 3.0 or above in your project, change compile to implementation. like below:

implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
Darush
  • 11,403
  • 9
  • 62
  • 60
  • 1
    @MaximeClaude If you are using Kotlin, replace annotationProcessor with kapt. Also make sure you add both lines. – Darush Oct 25 '17 at 11:32
  • Thanks for the input @Darush. I just discovered that the problem is that I use it out of an/a activity/fragment. I pass a view to a constructor of that class and try to bind it there. Maybe this is why it doesn't work. – Maxime Claude Oct 25 '17 at 12:00
  • @Darush thank you, I replaced the annotationProcessor with kapt and it worked for me. – jojemapa Jan 02 '18 at 21:49
  • 1
    Remember add this line in onCreateView: ButterKnife.bind(this, mRootView); – O Thạnh Ldt Dec 13 '18 at 04:20
  • I don't understand the class where I got null view is Java, just some of my classes are written in Kotlin. "Replace annotationProcessor with kapt" works, however. – Nguyen Tan Dat Aug 23 '20 at 15:51
3

I started getting NPE errors when, on an existing project, I added support for DataBinding and Kotlin.

I had:

annotationProcessor 'com.jakewharton:butterknife-compiler:x.x.x'

..and replaced with:

kapt "com.jakewharton:butterknife-compiler:x.x.x"

Jorge E. Hernández
  • 2,800
  • 1
  • 26
  • 50
2

Yep, Butterknife by Jake Wharton has been updated to 8.0.1

Please refer at his git account for steps Butterknife Git

On final note : Make sure the line apply plugin ... is placed somewhere at the top of the file.

ralphgabb
  • 10,298
  • 3
  • 47
  • 56
0

I got the same exception. In my case I forgot to add jcenter() repository in my app-module build.gradle file.

buildscript {
    repositories {
        mavenCentral()
        //this was missed
        jcenter()
    }
    dependencies {
        ...
    }
}
Jackky777
  • 644
  • 12
  • 19