SQLiteOpenHelper空指针异常

编程入门 行业动态 更新时间:2024-10-28 08:21:17
本文介绍了SQLiteOpenHelper空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我实现我的Andr​​oid应用程序缓存并使用SQLite数据库来存储服务器的响应。用于处理DB中的类定义如下。此外,它具有用作一个数据库帮手SQLiteOpenHelper类

I am implementing a cache for my android app and using SQLite Database to store server responses. The class for handling the DB is defined below. Also it has a SQLiteOpenHelper class which is used as a database helper.

类CacheDB是从所谓的    公共静态CacheDB cacheDB =新CacheDB(上下文);

The class CacheDB is called from public static CacheDB cacheDB = new CacheDB(context);

和我也调用该方法    cacheDB.fetchDatafromDB(诗经);

and I am also calling the method cacheDB.fetchDatafromDB("songs");

有类的code为如下。

The code for the classes is given below.

package com.songs.lookup; import java.util.ArrayList; import java.util.List; import android.annotation.SuppressLint; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteStatement; public class CacheDB { public CacheDB(Context context){ System.out.println("Before constructing "); this.context = context; this.dbHelper = new CacheDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION); System.out.println("After constructing "); } private Context context; private CacheDBHelper dbHelper; private static final String DATABASE_NAME = ""; private static final int DATABASE_VERSION = 1; private static final String song_TABLE_NAME = "songs"; private static final String tune_TABLE_NAME = "tunes"; private static final String person_TABLE_NAME = "persons"; private static final String COLUMN_NAME = "name"; private static final String song_TABLE_CREATE = "CREATE TABLE " + song_TABLE_NAME + " (" + COLUMN_NAME + " TEXT);"; private static final String tune_TABLE_CREATE = "CREATE TABLE " + tune_TABLE_NAME + " (" + COLUMN_NAME + " TEXT);"; private static final String person_TABLE_CREATE = "CREATE TABLE " + person_TABLE_NAME + " (" + COLUMN_NAME + " TEXT);"; class CacheDBHelper extends SQLiteOpenHelper{ public CacheDBHelper(Context context, String name, CursorFactory factory, int version) { super(context, DATABASE_NAME, null, DATABASE_VERSION); // System.out.println("Before the cachedbhelper"); System.out.println("After the cachedbhelper"); } @Override public void onCreate(SQLiteDatabase db) { System.out.println("Here inside the oncreate of cacheDBHelper"); db.execSQL(song_TABLE_CREATE); db.execSQL(tune_TABLE_CREATE); db.execSQL(person_TABLE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } @SuppressLint("NewApi") public void performOperation(String Operation, String table, ArrayList<String> array1) { SQLiteDatabase db = dbHelper.getWritableDatabase(); String INSERT = "insert into " + table + " (" + COLUMN_NAME + ") values (?)"; String DELETE = "delete from " + table; String FETCH = "select DISTINCT(" + COLUMN_NAME + "from " + table + ")"; db.beginTransaction(); SQLiteStatement dbStmt = dbpileStatement(Operation == "INSERT" ? INSERT : DELETE); if(Operation == "INSERT") { int aSize = array1.size(); for (int i = 0; i < aSize; i++) { dbStmt.bindString(1, array1.get(i)); dbStmt.executeInsert(); } } if(Operation == "DELETE") { dbStmt.executeUpdateDelete(); } if(Operation == "SELECT") { fetchDatafromDB(table); } db.setTransactionSuccessful(); db.endTransaction(); try { db.close(); } catch (Exception e) { e.printStackTrace(); } } public List<String> fetchDatafromDB(String table) { CacheDBHelper dbHelper = new CacheDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION); SQLiteDatabase db = dbHelper.getWritableDatabase(); List<String> list = new ArrayList<String>(); // Select All Query String selectQuery = "SELECT * FROM " + table; Cursor cursor = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (cursor.moveToFirst()) { do { cursor.getString(0); } while (cursor.moveToNext()); } // return contact list return list; } }

我收到以下堆栈跟踪

I am getting the following stack trace

04-21 00:55:15.188: E/AndroidRuntime(790): Caused by: java.lang.NullPointerException 04-21 00:55:15.188: E/AndroidRuntime(790): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 04-21 00:55:15.188: E/AndroidRuntime(790): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) 04-21 00:55:15.188: E/AndroidRuntime(790): at com.songs.lookup.CacheDB.fetchDatafromDB(CacheDB.java:124) 04-21 00:55:15.188: E/AndroidRuntime(790): at com.songs.lookup.LookUpData.getData(LookUpData.java:25) 04-21 00:55:15.188: E/AndroidRuntime(790): at com.songs.MainActivity2.onCreate(MainActivity2.java:64) 04-21 00:55:15.188: E/AndroidRuntime(790): at android.app.Activity.performCreate(Activity.java:5008) 04-21 00:55:15.188: E/AndroidRuntime(790): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 04-21 00:55:15.188: E/AndroidRuntime(790): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

在哪里这里是什么问题?

Where is the problem here?

推荐答案

我有一个类似的问题早,我的是因为我想那些已经打开了它前面的方法中打开一个数据库。我不知道,如果是有道理的,但我可以在这里看到你所说的 dbHelper.getWritableDatabase()方法两次没有在第一时间后关闭数据库。我认为这可能是你的问题的原因,因为数据库是由previous方法锁定,因此误差

I had a similar problem earlier and mine was because I was trying to open a database within a method that had already opened it earlier. I don't know if that makes sense, but I can see here where you call the dbHelper.getWritableDatabase() method twice without closing the database after the first time. I think that could be the cause of your issue, as the database is locked by the previous method, hence the error

at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)

也不太清楚,如果我是100%正确的在这里,但希望这有助于。欢呼声

Not too sure if I'm a 100% correct here, but hope this helps. cheers

更多推荐

SQLiteOpenHelper空指针异常

本文发布于:2023-08-02 17:48:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279853.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   异常   SQLiteOpenHelper

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!