You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用Retrofit获取数据后插入SQLite报空对象错误,求排查

Troubleshooting Null Pointer When Saving Retrofit Data to SQLite

Hey there! Let's figure out why you're hitting that null pointer exception when saving data to SQLite—especially since the data shows up fine in your TextView, so Retrofit's doing its job right. Here are the most likely culprits and how to fix them:

1. You're passing a null user object (or null fields) to saveUser

Even though the TextView displays the data, it might be handling nulls gracefully (like showing an empty string or using toString() on a non-null parent object) while your saveUser method isn't.

To confirm:

  • Add logs or set a breakpoint right before calling saveUser to check if the user object or its fields are null:
    Log.d("SaveUserCheck", "User object: " + (user == null ? "NULL" : user.toString()));
    Log.d("SaveUserCheck", "User email: " + (user.getEmail() == null ? "NULL" : user.getEmail()));
    sqliteHandler.saveUser(user);
    
  • If you're pulling data from a Retrofit response list (like response.body().getUsers()), make sure you're not accessing an index that doesn't exist (e.g., get(0) when the list is empty).

2. Your saveUser method lacks null checks

It’s common for SQLite insertions to fail if you pass null values without handling them, especially if your database schema doesn’t allow nulls for certain columns.

Example problematic saveUser code:

public void saveUser(User user) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_NAME, user.getName()); // Crashes if user.getName() is null
    values.put(KEY_EMAIL, user.getEmail()); // Same issue here
    db.insert(TABLE_USERS, null, values);
    db.close();
}

Fixes:

  • Add a guard clause at the start of saveUser to catch null objects:
    public void saveUser(User user) {
        if (user == null) {
            Log.e("SQLiteError", "Attempted to save a null User object!");
            return;
        }
        // Rest of your code
    }
    
  • Handle null fields by setting default values:
    values.put(KEY_NAME, user.getName() != null ? user.getName() : "");
    values.put(KEY_EMAIL, user.getEmail() != null ? user.getEmail() : "");
    

3. Your SQLiteHandler instance is null

Double-check that you properly initialized your SQLiteHandler before calling saveUser. It’s easy to forget this step, especially if you’re initializing it in a lifecycle method (like onCreate) and accidentally skip it:

// Wrong: Uninitialized instance
SQLiteHandler sqliteHandler;

// Correct: Initialize with a valid context
SQLiteHandler sqliteHandler = new SQLiteHandler(getApplicationContext());

4. Use the error log to pinpoint the exact issue

Your error screenshot should show the line number where the null pointer occurs. For example:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.yourapp.User.getUsername()' on a null object reference

This tells you exactly that either the User object is null, or getUsername() is returning null. Follow that line back to see where the null is coming from.


内容的提问来源于stack exchange,提问作者ali

火山引擎 最新活动