SQLiteOpenHelper空指针异常 [英] SQLiteOpenHelper null pointer exception

查看:135
本文介绍了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 = 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;
    }
}

我收到以下堆栈跟踪

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空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆