我试图在一个方法中存储特定的列数据,然后在另一个类String []中调用该方法,微调器将访问该数组以显示列行,以供用户选择,然后将其存储在另一个数据库中。

我得到nulpointerexception,当我尝试打开数据库时,数据库被锁定。对不起,听起来像是一个完整的业余爱好者,相对于android来说还比较新。预先感谢您的任何帮助。

这是我在主类中调用getInstance()和getCPnames()时的代码

    String[] carParks = CarParkDb.getInstance().getCpnames();


这是我的数据库代码:

package com.example.parkangel;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class CarParkDb {


    public static final String KEY_ID = "_id";
    public static final String KEY_CPNAME = "cpname";
    public static final String KEY_COST = "cost";


    private static final String DATABASE_NAME = "CPDB";
    private static final String DATABASE_TABLE = "CPTable";
    private static final int DATABASE_VERSION = 1;

    private CPDbHelper cpdbHelper;

    private Context ourContext;
    private SQLiteDatabase ourDatabase;
    private static CarParkDb instance;


    private static class CPDbHelper extends SQLiteOpenHelper{

        public CPDbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }



@Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                    KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    KEY_CPNAME + " TEXT NOT NULL, " + KEY_COST + " TEXTNOT NULL);");

            db.execSQL("INSERT INTO " + DATABASE_TABLE + " Values('1','LearningResource Center','2');");
            db.execSQL("INSERT INTO " + DATABASE_TABLE + " Values ('2','ParkandRide','1');");
            db.execSQL("INSERT INTO " + DATABASE_TABLE + " Values ('3','deHavilland Campus','2');");
            db.execSQL("INSERT INTO " + DATABASE_TABLE + " Values('4','MultiStorey Building','2');");
            db.execSQL("INSERT INTO " + DATABASE_TABLE + " Values('5','Reception','2');");

        }

        public void onOpen(final SQLiteDatabase db) {
            super.onOpen(db);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);
        }
    }

        public CarParkDb (final Context c)
        {
            this.cpdbHelper= new CPDbHelper(c);
            establishDb();
            //ourContext = c;
}
        public void establishDb()
        {
            if (this.ourDatabase == null)
            {
                this.ourDatabase = this.cpdbHelper.getWritableDatabase();
            }

        }

        public CarParkDb() {
            // TODO Auto-generated constructor stub
        }
        public CarParkDb open() throws SQLException
        {
            System.out.println ("running open");
            cpdbHelper = new CPDbHelper(ourContext);
            ourDatabase = cpdbHelper.getWritableDatabase();
            return this;
        }

        public void close()
        {
            ourDatabase.close();
        }

        /*public long createEntry(String cpname, String cost){
            ContentValues cv = new ContentValues();
            cv.put(KEY_CPNAME, cpname);
            cv.put(KEY_COST, cost);
            return ourDatabase.insert(DATABASE_TABLE, null, cv);
        }*/

        public String getData() {
            // TODO Auto-generated method stub
            //open();
            String[] columns = new String[] {KEY_ID, KEY_CPNAME, KEY_COST};
            Cursor  c = ourDatabase.query(DATABASE_TABLE, columns, null,null,null, null, null);
            String result = " ";

            int iRow = c.getColumnIndexOrThrow(KEY_ID);
            int iCpname = c.getColumnIndexOrThrow(KEY_CPNAME);
            int iCost = c.getColumnIndexOrThrow(KEY_COST);

            for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
                result = result + c.getString(iRow) + " " +c.getString(iCpname) + " " + c.getString(iCost) + " " + "\n";
                c.close();
                ourDatabase.close();
            }
            return result;
        }

        public static CarParkDb getInstance()
        {
            synchronized(CarParkDb.class)
        {
            if (instance == null)
            {
                instance = new CarParkDb();
            }
            return instance;
        }
        }

    public String[] getCpnames()
        {
            //open();
            if (ourDatabase == null) System.out.println ("is null");
            Cursor c = null;

            ArrayList<String> list = new ArrayList<String>();
            ourDatabase = cpdbHelper.getReadableDatabase();
            //SQLiteDatabase db = cpdbHelper.getReadableDatabase();
            String query = "SELECT " + KEY_CPNAME + " FROM " + DATABASE_TABLE;



            {
            c = this.ourDatabase.rawQuery(query, null);
            int iCpname = c.getColumnIndexOrThrow(KEY_CPNAME);
            if (c.moveToFirst())
                {
                    do
                    {
                    list.add(c.getString(iCpname));;
                    }
                    while (c.moveToNext());
                }
                if (c != null && !c.isClosed())
                {
                c.close();
                ourDatabase.close();
                }
                return list.toArray(new String[]{});
            }
        }

}

**LogCat**

    03-12 01:32:39.759: E/AndroidRuntime(4176): Caused by:java.lang.NullPointerException
    03-12 01:32:39.759: E/AndroidRuntime(4176):

    at com.example.parkangel.CarParkDb.getCpnames(CarParkDb.java:191)
    03-12 01:32:39.759: E/AndroidRuntime(4176):

    at com.example.parkangel.BookTicket.<init>(BookTicket.java:22)
    03-12 01:32:39.759: E/AndroidRuntime(4176):

    at java.lang.Class.newInstanceImpl(Native Method)
    03-12 01:32:39.759: E/AndroidRuntime(4176):

    at java.lang.Class.newInstance(Class.java:1208)

最佳答案

尝试使用以下方法更新您的getInstance()代码:

public static CarParkDb getInstance(Context c) // <-- added context as parameter
{
    synchronized(CarParkDb.class)
    {
        if (instance == null)
        {
            instance = new CarParkDb(c); // <-- used context in constructor
        }
        return instance;
    }
}


问题似乎是您正在使用什么都不做的构造函数(用于CarParkDb)。您有另一个构造函数,该构造函数采用Context作为参数,并初始化稍后使用的某些对象。

07-24 09:22