Android应用个人页NPE求助:LoginModel.getUserInfo()空指针异常
问题描述
账号可正常登录运行应用,但个人资料页账号信息为空,触发空值崩溃。排查时遇到**NullPointerException(NPE)**错误,怀疑是ButterKnife的Bug,本人为Android新手,求解决办法。
相关代码与日志
fragment_account.xml
<?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>
AccountFragment.java(部分代码)
@BindView(R.id.user_info) protected TextView userInfo;
LoginModel.java
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 + '}'; } }
Logcat错误日志
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
页面异常截图

核心问题
调用LoginModel.getUserInfo()时触发空指针,本质是LoginModel对象本身为空,导致无法获取userInfo字段内容,最终页面信息为空并崩溃。
解决方案
1. 先排除ButterKnife的嫌疑
从日志看,空指针不是userInfo TextView,而是LoginModel实例为空。先确认:
- 你是从SharedPreferences、ViewModel还是网络请求结果中获取LoginModel?有没有确保实例已正确初始化?
- 如果是网络请求后赋值,要等请求成功、数据解析完成后再更新UI。
2. 强制判空,避免崩溃
在调用getUserInfo()前必须双重判空:
// 假设你从本地存储或ViewModel获取LoginModel实例 LoginModel loginModel = getLoginModelFromStorage(); if (loginModel != null && loginModel.getUserInfo() != null) { // 安全更新UI,替换成你实际要展示的字段 userInfo.setText(loginModel.getUserInfo().getFullName()); } else { // 为空时显示默认提示 userInfo.setText("未获取到用户信息"); }
3. 检查数据解析是否正确
确认接口返回的JSON中,UserInfo字段名和LoginModel里的@SerializedName("UserInfo")大小写完全一致(Gson解析大小写敏感),如果接口返回的是userInfo小写开头,解析后userInfo就会为空。
4. ButterKnife基础检查(以防万一)
虽然日志不是TextView空,但还是确认下:
- Fragment的
onViewCreated方法中是否调用了ButterKnife.bind(this, view)? - 布局里的
user_infoID有没有拼写错误? - 清理项目缓存后重新编译(Build -> Clean Project -> Rebuild Project)。
内容的提问来源于stack exchange,提问作者user19718250
相关产品推荐
相关产品推荐

