java.lang.NullPointerException: Cannot invoke "Object.hashCode()" because "key"

Posted yzx-sir

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java.lang.NullPointerException: Cannot invoke "Object.hashCode()" because "key" 相关的知识,希望对你有一定的参考价值。

 

 

 

映射文件需要调整,mybatis-config.xml里面的

如何使用其id获取单个SQLite行数据?

我已成功将SQLite数据检索到ListView。现在我正在尝试实现OnItemClickListener以在Dialog中显示数据但我收到此错误

2019-04-08 18:42:53.020 20591-20591/com.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app, PID: 20591
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:292)
        at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:262)
        at com.app.SQLiteAdaptor.GetUserByUserId(SQLiteAdaptor.java:99)
        at com.app.OrderHistory$1.onItemClick(OrderHistory.java:64)
        at android.widget.AdapterView.performItemClick(AdapterView.java:318)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1181)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3165)
        at android.widget.AbsListView$3.run(AbsListView.java:4147)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:173)
        at android.app.ActivityThread.main(ActivityThread.java:6634)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:822)

这是我需要检索行的地方


 @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                final Dialog fullscreenDialog = new Dialog(getApplicationContext(), R.style.Dialog);
                    fullscreenDialog.setContentView(R.layout.dialog_oh);


                SQLiteAdaptor db = new SQLiteAdaptor(c);
                ArrayList<HashMap<String, String>> userList = db.GetUserByUserId(i); //Line 64
                HashMap<String, String> hashDetails = userList.get(0);

                     //Use this index accordingly
                    tvit.setText(hashDetails.get("item"));
                    tvpr.setText(hashDetails.get("price"));
                    tvqu.setText(hashDetails.get("quantity"));
                    tvad.setText(hashDetails.get("address"));
                    tvna.setText(hashDetails.get("name"));
                    tvem.setText(hashDetails.get("email"));
                    tvtim.setText(hashDetails.get("time"));

fullscreenDialog.show();
}
});

以下是id方法片段的SQLiteAdaptor获取行数据


 public ArrayList<HashMap<String, String>> GetUserByUserId(int userid ){
        SQLiteDatabase db = this.getReadableDatabase(); \\Line 99
        ArrayList<HashMap<String, String>> userList = new ArrayList<>();
        String  query = "SELECT item, price, quantity, name, address, email, orderid FROM "+ Table_Name;

        Cursor cursor = db.query(Table_Name, new String[]{COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8, COL_9}, COL_1+ "=?",new String[]{String.valueOf(userid)},null, null, null, null);
        if (cursor.moveToNext()){
            HashMap<String,String> user = new HashMap<>();
            user.put("item",cursor.getString(cursor.getColumnIndex(COL_2)));
            user.put("price",cursor.getString(cursor.getColumnIndex(COL_3)));
            user.put("quantity",cursor.getString(cursor.getColumnIndex(COL_4)));
            user.put("name",cursor.getString(cursor.getColumnIndex(COL_5)));
            user.put("address",cursor.getString(cursor.getColumnIndex(COL_6)));
            user.put("email",cursor.getString(cursor.getColumnIndex(COL_7)));
            user.put("orderid",cursor.getString(cursor.getColumnIndex(COL_8)));
            user.put("time",cursor.getString(cursor.getColumnIndex(COL_9)));
            userList.add(user);
        }
        return  userList;
    }

并且它不是空指针异常,因为我在Listview中检索数据时没有收到任何错误

或者任何其他如何使用其id检索行的方法

任何帮助和解决方案表示赞赏

提前致谢

答案

试试这个

SQLiteAdaptor db = new SQLiteAdaptor(view.getContext());

以上是关于java.lang.NullPointerException: Cannot invoke "Object.hashCode()" because "key" 的主要内容,如果未能解决你的问题,请参考以下文章

亲測,Eclipse报&quot;An error has occurred,See error log for more details. java.lang.NullPointerExce

来自 LayerDrawable 的异常

Microsoft Access 和 Java JDBC-ODBC 错误

如何使用其id获取单个SQLite行数据?