简体   繁体   中英

Null Object reference on room DAO returned object

I am having an issue understanding why i get a null object exception.

My call to the DAO is in onCreate:

  AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                WDatabase wdatabase = WDatabase.getInstance(MyApplication.getAppContext());
                NDao nDao = wDatabase.nDao();
                MNode mNode = new MNode();
                mNode = nDao.getMNodeByID(516);
                Log.d("NTest", "nData: "+mNode.getNID());
            }
        });

The method in my DAO is

//Get by id
    @Query("SELECT * FROM MNode WHERE nodeID= :nodeID")
    MNode getMNodeByID(int nodeID);

In my Model the variable is, int nodeID

What am i doing wrong to return a null pointer? The Database exists, the node exists in the database? But yet i still get the null object?

you have to write the query like this

@Query("SELECT * FROM MNode WHERE nodeID= :nodeID")
int getMNodeByID(int nodeID);

and it will return the id of your required query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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