Reputation: 4388
I am trying to use ButterKnife
within my Android project.
I have an Activity
that implements an Interface
. I am using MVP pattern.
I am trying to get text from the EditText
but it throws a
null pointer exception.
Here is my simple code.
Gradle dependencies
compile 'com.jakewharton:butterknife:8.8.1'
Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rao.myapplication.LoginActivity">
<EditText
android:id="@+id/etUseName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/etUseName"/>
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:onClick="loginClick"
app:layout_constraintTop_toBottomOf="@+id/etPassword"/>
</android.support.constraint.ConstraintLayout>
Activity
package com.example.rao.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.rao.myapplication.Presenter.LoginActivityPresenter;
import com.example.rao.myapplication.View.LoginActivityView;
import butterknife.BindView;
import butterknife.ButterKnife;
public class LoginActivity extends AppCompatActivity implements LoginActivityView {
@BindView(R.id.etUseName)
EditText userNameET;
@BindView(R.id.etPassword)
EditText passwordET;
@BindView(R.id.btnLogin)
Button loginBtn;
private LoginActivityPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
passwordET.setText("abcd"); // Fails here as well
presenter = new LoginActivityPresenter(this);
}
@Override
public void navigateToListActivity() {
}
@Override
public void loginFailed() {
}
public void loginClick(View view) {
Toast.makeText(this, userNameET.getText().toString(), Toast.LENGTH_SHORT ).show(); // Fails here
Toast.makeText(this, "test", Toast.LENGTH_SHORT ).show();
}
}
Here is the error log
FATAL EXCEPTION: main Process: com.example.rao.myapplication, PID: 24847 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rao.myapplication/com.example.rao.myapplication.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6682) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference at com.example.rao.myapplication.LoginActivity.onCreate(LoginActivity.java:34) at android.app.Activity.performCreate(Activity.java:6942) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6682) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Could you please guide me in the right direction on this, please.
Upvotes: 2
Views: 3980
Reputation: 49
If you forgot to add ButterKnife.bind(this);
after setContentView, you will get NullPointerException.
Upvotes: 2
Reputation: 841
Put this in the gradle file and check. Here is the link for official documentation.
GRADLE
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
Upvotes: 2
Reputation: 9056
add below line also in your gradle file....
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.+'//add this line also
Hope it solve your problems....
Upvotes: 7