简体   繁体   English

从SQLite rowID获取NullPointerException

[英]NullPointerException from getting SQLite rowID

As the title says, I'm getting a NullPointerException and I have no idea why. 如标题所示,我收到了NullPointerException ,我不知道为什么。 I've been through the code several times and printed out the data to the logcat before it is entered and it looks fine. 我已经遍历了几次代码,并在输入数据之前将数据打印到了logcat上,看起来还不错。 Before I enter the code I just want to let you know that I am using a RecyclerView if it wasn't obvious. 在输入代码之前,我只是想让您知道,如果不是显而易见的话,我正在使用RecyclerView

// Implement OnLongClick listener. Long Clicked items is removed from list.
@Override
public boolean onLongClick(View view) {
    ViewHolder holder = (ViewHolder) view.getTag();
    if (view.getId() == holder.mNameTextView.getId()) {

        mDataset.remove(holder.getPosition());

        Log.i("NULL POINTER", (holder.mNameTextView.getText().toString()));
        String temp = sqLiteDBadapter.getRowid(holder.mNameTextView.getText().toString()); // --- It fails on this line
        Long temp2 = Long.parseLong(temp);

        sqLiteDBadapter.deleteScore(temp2);

        // Call this method to refresh the list and display the "updated" list
        notifyDataSetChanged();

        Toast.makeText(sContext, "Item " + holder.mNameTextView.getText() + " has been removed from list",
                Toast.LENGTH_SHORT).show();

    }
    return false;
}

Here is part of the SQLiteDBadapter: 这是SQLiteDBadapter的一部分:

public String getRowid(String date) {
    mDbHelper = new DatabaseHelper(mCtx);
    SQLiteDatabase db=mDbHelper.getReadableDatabase();

    Cursor c = db.rawQuery("SELECT * from " + DATABASE_TABLE + " WHERE date = ?" , new String[] { date });
    if (c.moveToFirst()){
        long temp;
        temp = c.getLong(c.getColumnIndex(KEY_ROWID));
        rowID = String.valueOf(temp);
        Log.i("----_ROW ID = ", rowID);
    }else  if (!c.moveToFirst())
        Log.i("CURSOR ERROR", " CURSOR INDEX MOST LIKELY 0");
    else
        c.moveToFirst();

    return rowID;
}

I declare rowID as String rowID; 我将rowID声明为String rowID; ealier in the code 代码中的更安全

As you can see here: Database SQLite Fiddle the code for getting the row works perfectly fine. 如您在此处看到的: Database SQLite Fiddle用于获取行的代码非常正常。

Also the Log.i NULL POINTER prints the output right before it crashes: I/NULL POINTER﹕ Nov 2, 2014 11:51:14 AM and that is the correct format according to the Fiddle Log.i NULL POINTER Log.i在崩溃之前立即输出输出: I/NULL POINTER﹕ Nov 2, 2014 11:51:14 AM ,根据Fiddle说法,这是正确的格式

11-02 12:29:30.917      419-419/com.archeryscoresmaterialdesign W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4160f620)
11-02 12:29:30.927      419-419/com.archeryscoresmaterialdesign E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.archeryscoresmaterialdesign, PID: 419
java.lang.NullPointerException
        at com.archeryscoresmaterialdesign.RecyclerViewAdapter.onLongClick(RecyclerViewAdapter.java:90)
        at android.view.View.performLongClick(View.java:4476)
        at android.widget.TextView.performLongClick(TextView.java:8362)
        at android.view.View$CheckForLongPress.run(View.java:18451)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5171)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:837)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
        at dalvik.system.NativeStart.main(Native Method)

Within this code 在此代码内

if (c.moveToFirst()){
    long temp;
    temp = c.getLong(c.getColumnIndex(KEY_ROWID));
    rowID = String.valueOf(temp);
    Log.i("----_ROW ID = ", rowID);
}else  if (!c.moveToFirst())
    Log.i("CURSOR ERROR", " CURSOR INDEX MOST LIKELY 0");
else
    c.moveToFirst();

you might end up never assigning anything to rowID and if you declared it simply String rowID; 您可能最终将永远不会为rowID分配任何内容,如果仅声明为String rowID; not String rowID = new String(); 不是 String rowID = new String(); it stays null. 它保持为空。 Is this a possibility ? 这有可能吗?

Are you corectly creating sqLiteDBadapter? 您是否正在核心地创建sqLiteDBadapter?

I suppose you have created a subclass of SQLiteOpenHelper . 我想您已经创建了SQLiteOpenHelper的子类。 You are trying to call method on Object, which is null. 您正在尝试在Object上调用方法,该方法为null。 Try this: 尝试这个:

SQLiteDBadapter db = new SQLiteDBadapter(ApplicationContext);

Now you should be able to call any methods on it. 现在,您应该可以在其上调用任何方法了。 And after you are done with reading, writing, tec.. dont forget to close the DB connection. 在完成读写之后,不要忘记关闭数据库连接。

db.close();

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

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