SQLiteOpenHelper空指针异常

时间:2013-04-21 06:43:23

标签: android sqlite

我正在为我的Android应用程序实现一个缓存,并使用SQLite数据库来存储服务器响应。处理DB的类定义如下。它还有一个SQLiteOpenHelper类,用作数据库助手。

从中调用CacheDB类     public static CacheDB cacheDB = new CacheDB(context);

我也在调用这个方法     cacheDB.fetchDatafromDB( “歌曲”);

下面给出了类的代码。

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 = db.compileStatement(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;
    }
}

我得到以下堆栈跟踪

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)

这里的问题在哪里?

2 个答案:

答案 0 :(得分:6)

我之前遇到过类似的问题,因为我试图在之前已经打开它的方法中打开一个数据库。我不知道这是否有意义,但我可以在这里看到你在没有关闭数据库的情况下第一次调用dbHelper.getWritableDatabase()方法两次。 我认为这可能是您的问题的原因,因为数据库被前一个方法锁定,因此错误

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

不太确定我是否100%正确,但希望这会有所帮助。欢呼声

答案 1 :(得分:0)

传递给SQLiteOpenHelper构造函数的Context是null。