Odai Mohammed
Odai Mohammed

Reputation: 319

Implementing ButterKnife giving NullPointer Exception

I'm trying to implement the ButterKnife library in a small project, how ever my app crashes when it starts, details:

here is my MainActivity.java :

    package com.example.odai.playwithme;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;

    import butterknife.BindView;
    import butterknife.ButterKnife;

    public class MainActivity extends AppCompatActivity {

        @BindView(R.id.hello_world) TextView hello;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ButterKnife.bind(this);

            hello.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     Toast.makeText(getApplicationContext(), "Hello",
                     Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

my activity_main.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        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"
        tools:context="com.example.odai.playwithme.MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:id="@+id/hello_world"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />
   </RelativeLayout>

my build.gradle :

    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.3"

       defaultConfig {
           applicationId "com.example.odai.playwithme"
           minSdkVersion 17
           targetSdkVersion 23
           versionCode 1
           versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'),
                'proguard-rules.pro'
           }
       }
   }

   dependencies {
       compile fileTree(dir: 'libs', include: ['*.jar'])
       testCompile 'junit:junit:4.12'
       compile 'com.android.support:appcompat-v7:23.4.0'
       compile 'com.jakewharton:butterknife:8.0.1'
   }

and this is the error I get whenever I try to run my app :

05-22 20:32:52.019 24076-24076/com.example.odai.playwithme E/AndroidRuntime: FATAL EXCEPTION: main
         Process: com.example.odai.playwithme, PID: 24076
         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.odai.playwithme/com.example.odai.playwithme.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2484)
             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
             at android.app.ActivityThread.access$900(ActivityThread.java:150)
             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
             at android.os.Handler.dispatchMessage(Handler.java:102)
             at android.os.Looper.loop(Looper.java:168)
             at android.app.ActivityThread.main(ActivityThread.java:5845)
             at java.lang.reflect.Method.invoke(Native Method)
             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
             at com.example.odai.playwithme.MainActivity.onCreate(MainActivity.java:22)
             at android.app.Activity.performCreate(Activity.java:6248)
             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544) 
             at android.app.ActivityThread.access$900(ActivityThread.java:150) 
             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394) 
             at android.os.Handler.dispatchMessage(Handler.java:102) 
             at android.os.Looper.loop(Looper.java:168) 
             at android.app.ActivityThread.main(ActivityThread.java:5845) 
             at java.lang.reflect.Method.invoke(Native Method) 
             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

any ideas?

Upvotes: 3

Views: 1569

Answers (2)

gatlingxyz
gatlingxyz

Reputation: 753

I had this problem a while back, too, and actually just tried again to see if it was just a one time thing. Apparently the newer ButterKnife requires a bit more stuff in your build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    compile 'com.jakewharton:butterknife:8.0.1'
    apt 'com.jakewharton:butterknife-compiler:8.0.1'
}

(https://github.com/JakeWharton/butterknife)

Upvotes: 4

Pro Mode
Pro Mode

Reputation: 1463

Remove @Bindview line and do the following to bind elements properly:

  1. Click on your layout name in setContentView line
  2. Press alt+enter , it will bring insert options
  3. Click generate butterknief injections
  4. Choose elements that you want to bind
  5. Click ok. And it will work. You can also set onclick events directly from there while choosing the views to bind.

Upvotes: 0

Related Questions