gpt4 book ai didi

java - Android NullPointerException 和 GetDatabaseLocked

转载 作者:行者123 更新时间:2023-12-01 23:08:49 24 4
gpt4 key购买 nike

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

我收到 nulpointerException,当我尝试 open() 数据库时,数据库被锁定。抱歉,听起来像是一个完全业余的人,对 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)

最佳答案

数据库没有被锁定。 “锁定”想法只是名为 getDatabaseLocked() 的方法中的 NPE,它是由传递给 SQLiteOpenHelper 的 null Context 引起的 在构造函数中,通过 getWritableDatabase()getReadableDatabase() 来体现。

要修复 getDatabaseLocked() 中的 NPE,请确保 open() 中的 ourContext 不为 null 。截至目前,您尚未初始化 ourContext,它始终为 null。取消构造函数中 //ourContext = c; 的注释,并将其移至 builtDb() 上方,删除另一个无参数构造函数并传入 Context code> 例如,Merlevede 建议的那样。

您问题中的 NPE 堆栈跟踪是当您调用 getCpNames() 并注释掉 open() 并尝试调用 null< 上的方法时 ourDatabase 对象引用。修复后取消注释 open() 。堆栈跟踪还表明您正在尝试在对象初始化阶段调用 getCpNames()初始化成员变量时。这可能还为时过早,例如,在 onCreate() 之前, Activity 不能用作 Context

您接下来会遇到的代码中的其他一些内容:

  • onCreate() 中:+ KEY_COST + "TEXTNOT NULL) - 在 TEXTNOT< 之间添加空格

  • getData() 中:您将在访问游标的循环中关闭游标和数据库。在循环之后执行此操作。该代码不应编译,因为如果 moveToFirst() 返回 false,您不会从非 void 函数返回任何内容。

关于java - Android NullPointerException 和 GetDatabaseLocked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22340551/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com