空指针应该很容易解决,但我不明白为什么我得到NullPointerException
这里。
public static void updateUserCity(String city, Context context) {
Realm realm = Realm.getInstance(context);
realm.beginTransaction();
RealmQuery<User> query = realm.where(User.class);
User user = query.findFirst();
if (user != null) {
user.setCity(new City(0, city, city)); // here it crashes
}
realm.commitTransaction();
}
而我的堆栈跟踪:
java.lang.NullPointerException
at io.realm.UserRealmProxy.setCity(UserRealmProxy.java:118)
at com.ratata.adapters.DatabaseAdapter.updateUserCity(DatabaseAdapter.java:38)
at com.ratata.services.userManagment.UserManager.updateCity(UserManager.java:30)
at com.ratata.dialogs.MainTabDialog.updateUserData(MainTabDialog.java:92)
at com.ratata.dialogs.MainTabDialog.updateTab(MainTabDialog.java:84)
at com.ratata.dialogs.MainTabDialog.access$000(MainTabDialog.java:32)
at com.ratata.dialogs.MainTabDialog$2.onClick(MainTabDialog.java:50)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:153)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
User
是不是null
的,所以我应该setCity
此对象。 我创造new City
与给定的数据。