简体   繁体   English

使用RealM时发生NullpointerException

[英]NullpointerException while using RealM

I am using the RealM Library to store my values in the database. 我正在使用RealM库将值存储在数据库中。 The problem is that since RealM does not support AutoIncrement i am trying to implement it by my own. 问题在于,由于RealM不支持AutoIncrement,所以我试图自己实现它。

This is my modal class: 这是我的模态课:

public class User extends RealmObject{

    @PrimaryKey
    private int id;
    private String name;
    private  int rollno;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getRollno() {
        return rollno;
    }

    public void setRollno(int rollno) {
        this.rollno = rollno;
    }


    public int getid() {
        return id;
    }

    public void setid(int id) {
        this.id = id;
    }

}

I my main class i have a function named insertValues in which i am inserting the data in the database. 我的主类中有一个名为insertValues的函数,其中将数据插入数据库中。

 public void insertValues(Realm realm,String name,int rollNo)
    {

        realm.beginTransaction();

        int nextId = (int)(realm.where(User.class).maximumInt("id") + 1); // this line gives me nullpointer error. Please help

        User user = realm.createObject(User.class);
        user.setName(name);
        user.setRollno(rollNo);
        realm.commitTransaction();
    }

My Logcat: 我的Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at io.realm.RealmQuery.maximumInt(RealmQuery.java:1421)
at realmsample.bd.com.realmsample.MainActivity.insertValues(MainActivity.java:105)
at realmsample.bd.com.realmsample.MainActivity$1.onClick(MainActivity.java:53)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
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)

See here , It's fairly similar as your issue, So as suggested, 请参阅此处 ,与您的问题非常相似,因此建议

If max related function is called on an empty results query or query's results are all null values, maximumInt will throw an exception. 如果在空结果查询中调用了与max相关的函数,或者查询的结果均为空值,则maximumInt将引发异常。

So use RealmQuery.max() instead. 因此,请改用RealmQuery.max()

The problem lies in your code while fetching max value of "id" field. 问题出在获取“ id”字段最大值的代码中。 When you add first object in the database, maximumInt("id") will be NULL . 当您在数据库中添加第一个对象时, maximumInt(“ id”)将为NULL

So you need to keep an if condition to prevent NPE. 因此,您需要保留if条件以防止NPE。

int nextId = 1;
if (realm.where(User.class).findAll().size() > 0) {
    nextId  = (int)(realm.where(User.class).maximumInt("id") + 1)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM