Search code examples
androidbutterknife

android:id="@+id/user_info" is null


I can run my application with an account. But account info seems like null on profile page. and so the app crash because of null value. I searched that there was a bug with butterknife. I am new to android. please help me to solve this problem. first I had an error NPE and I saw that id is null.

my fragment_account.xml is:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    android:fillViewport="true"
    android:background="@color/white"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/profile_image"
            android:layout_width="125dp"
            android:layout_height="125dp"
            android:layout_marginTop="45dp"
            android:src="@drawable/icon_face"
            android:padding="25dp"
            android:background="@drawable/cirlce"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="Üye No: 18598575"
            android:textColor="@color/profile_uye_no_color"
            android:fontFamily="@font/nunito_bold"
            android:textSize="@dimen/title_sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/constraintLayout6" />

        <TextView
            android:id="@+id/user_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="32dp"
            android:layout_marginStart="32dp"
            android:layout_marginTop="20dp"
            android:gravity="center_horizontal"
            android:textColor="#565656"
            android:fontFamily="@font/nunito_bold"
            android:textSize="@dimen/big_sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/user_name"
            tools:text="AppLogist Bilisim Teknolojileri San. ve Tic. Limited Sirketi" />

        <Button
            android:id="@+id/profile_edit"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginEnd="100dp"
            android:layout_marginStart="100dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/button_gradiant_blue"
            android:text="@string/account_edit_profile_button_text"
            android:textAllCaps="false"
            android:textColor="#ffffff"
            android:textSize="12sp"
            android:visibility="gone" />


        <LinearLayout
            android:id="@+id/provizyon_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:orientation="vertical"
            android:visibility="gone">

            <View
                android:layout_width="match_parent"
                android:layout_height="0.2dp"
                android:background="@color/grey_color" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginEnd="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginStart="15dp"
                android:layout_marginTop="10dp"
                android:drawableEnd="@drawable/next_3_kopya_2"
                android:drawableRight="@drawable/next_3_kopya_2"
                android:fontFamily="@font/nunito"
                android:text="@string/account_provizyon_text"
                android:textSize="@dimen/medium_sp" />

            <View
                android:layout_width="match_parent"
                android:layout_height="0.2dp"
                android:background="@color/grey_color" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/satin_alma_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="vertical"
            android:visibility="visible">

            <View
                android:id="@+id/view"
                android:layout_width="match_parent"
                android:layout_height="0.2dp"
                android:background="@color/grey_color" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginEnd="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginStart="15dp"
                android:layout_marginTop="10dp"
                android:drawableEnd="@drawable/next_3_kopya_2"
                android:drawableRight="@drawable/next_3_kopya_2"
                android:fontFamily="@font/nunito"
                android:text="@string/account_buyings"
                android:textSize="@dimen/medium_sp" />

            <View
                android:layout_width="match_parent"
                android:layout_height="0.2dp"
                android:background="@color/grey_color" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ihale_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="visible">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="15dp"
                android:layout_marginEnd="15dp"
                android:fontFamily="@font/nunito"
                android:textSize="@dimen/medium_sp"
                android:drawableRight="@drawable/next_3_kopya_2"
                android:drawableEnd="@drawable/next_3_kopya_2"
                android:text="@string/account_offers_text" />

            <View
                android:layout_width="match_parent"
                android:layout_height="0.2dp"
                android:background="@color/grey_color" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/cikis_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="visible">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginLeft="15dp"
                android:layout_marginStart="15dp"
                android:layout_marginTop="10dp"
                android:text="@string/account_exit_text"
                android:layout_marginRight="15dp"
                android:layout_marginEnd="15dp"
                android:fontFamily="@font/nunito"
                android:textSize="@dimen/medium_sp"
                android:drawableRight="@drawable/next_3_kopya_2"
                android:drawableEnd="@drawable/next_3_kopya_2"
                android:textColor="@color/orange_color" />

            <View
                android:layout_width="match_parent"
                android:layout_height="0.2dp"
                android:background="@color/grey_color" />

        </LinearLayout>

    </LinearLayout>

</ScrollView>

my AccountFragment.java is:

 @BindView(R.id.user_info)
    protected TextView userInfo;

my logcat is:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.applogist.filomotive.network.responese.model.user.UserModel com.applogist.filomotive.network.responese.model.login.LoginModel.getUserInfo()' on a null object reference

my profil page with "üye no..."Normally the first and last name should appear.

my LoginModel.java is:

public class LoginModel extends BaseModel {

    @SerializedName("LoginKey")
    private String loginKey;

    @SerializedName("LogoutTime")
    private Date logoutTime;

    @SerializedName("UserInfo")
    private UserModel userInfo;


    public String getLoginKey() {
        return loginKey;
    }

    public void setLoginKey(String loginKey) {
        this.loginKey = loginKey;
    }

    public Date getLogoutTime() {
        return logoutTime;
    }

    public void setLogoutTime(Date logoutTime) {
        this.logoutTime = logoutTime;
    }

    public UserModel getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserModel userInfo) {
        this.userInfo = userInfo;
    }

    @Override
    public String toString() {
        return "LoginModel{" +
                "loginKey='" + loginKey + '\'' +
                ", logoutTime=" + logoutTime +
                ", userInfo=" + userInfo +
                '}';
    }
}

my model cannot receive userInfo.


Solution

  • private UserModel userInfo = new UserModel();
    

    in LoginModel.java object creation is required as above. sorry for my poor info about this question