gpt4 book ai didi

java - 甚至不进入方法。错误 "invoke virtual method java.lang.list on a null object reference"

转载 作者:搜寻专家 更新时间:2023-10-30 23:45:39 24 4
gpt4 key购买 nike

我正在使用 SQLite 数据库创建一个时间表应用程序并且它运行良好,直到我阅读了一篇关于良好编码实践的文章。由于其中包含每个创建、读取、更新和删除语句,我有一个包含 1000 多行代码的 DatabaseHelper 文件。我离开并编辑了我的代码,以便每个表都有一个单独的类用于它的 CRUD 语句,但现在它不起作用。每次我运行该应用程序时,它都会崩溃并出现错误

在空对象引用上调用虚方法 java.lang.list Database.SschoolDay_CRUD(90% 是这样。恐怕至少 24 小时内我无法获得具体错误,但我要到明天下午才能访问具有运行该应用程序权限的电脑。)

我不想回滚到 1000 行以上的类(class)。因此,经过一些侦探工作后,我找出了出错的具体行。

我的 DatabaseHelper 运行这个方法

 SschoolDay_CRUD sschool_day;
List<SschoolDay_model_class> s;
public List<SschoolDay_model_class> getAllSSchoolDays() {
Log.v("Does it get here?", "YES");

s = sschool_day.getAllSSchoolDays();
Log.v("Does it get here?", "YES2");

return s;
}

然后我希望它在 SschoolDay_CRUD 类中运行 getAllSSchoolDays() 方法,如下所示:

/*
* getting all SchoolDays
*/
public List<SschoolDay_model_class> getAllSSchoolDays() {
Log.v("Peep", "BEFORE");
List<SschoolDay_model_class> school_day = new ArrayList<SschoolDay_model_class>();
Log.v("Peep", "after");
String selectQuery = "SELECT * FROM " + DatabaseHelper.TABLE_SSCHOOL_DAY;

Log.e(DatabaseHelper.LOG, selectQuery);

SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
SschoolDay_model_class sdy = new SschoolDay_model_class();
sdy.setSchool_day_id(c.getInt((c.getColumnIndex(DatabaseHelper.KEY_SSCHOOL_DAY_ID))));
sdy.setSchool_day(c.getString(c.getColumnIndex(DatabaseHelper.KEY_SSCHOOL_DAY)));
sdy.setUsed(c.getString(c.getColumnIndex(DatabaseHelper.KEY_SSCHOOL_DAY_USED)));
sdy.setNo_of_lessons(c.getInt(c.getColumnIndex(DatabaseHelper.KEY_SSCHOOL_DAY_NO_OF_LESSONS)));

// adding to SchoolDay list
school_day.add(sdy);
} while (c.moveToNext());

}
return school_day;
}

日志甚至不显示“BEFORE”。因此它似乎是

public List<SschoolDay_model_class> getAllSSchoolDays() {

有关额外信息,类 SschoolDay_model_class 如下所示:

package Database;

public class SschoolDay_model_class {

long school_day_id;
String school_day;
String used;
int no_of_lessons;

//constructors
public SschoolDay_model_class() {
}

public SschoolDay_model_class (long school_day_id, String school_day, String used, int no_of_lessons){
this.school_day_id = school_day_id;
this.school_day = school_day;
this.used = used;
this.no_of_lessons = no_of_lessons;

}


//setters
public void setSchool_day_id(long school_day_id){
this.school_day_id = school_day_id;
}

public void setSchool_day(String school_day){
this.school_day = school_day;
}

public void setUsed(String used) {
this.used = used;
}

public void setNo_of_lessons(int no_of_lessons){
this.no_of_lessons = no_of_lessons;
}

//getters
public long getSchool_day_id() {
return this.school_day_id;
}

public String getschool_day() {
return this.school_day;
}

public String getUsed() {
return this.used;
}

public int getNo_of_lessons() {
return this.no_of_lessons;
}

编辑:SschoolDay_CRUD 类是这样的:

       package Database;

import java.util.ArrayList;
import java.util.List;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SschoolDay_CRUD extends SQLiteOpenHelper {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
/*
* School Days will never be deleted, only set to used or not, which will determine their use.
* Therefore I have not written a delete statement for school day.
*/

/*
* Creating a School Day
*/


public long createSSchoolDay(SschoolDay_model_class school_day) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(DatabaseHelper.KEY_SSCHOOL_DAY, school_day.getschool_day());
values.put(DatabaseHelper.KEY_SSCHOOL_DAY_USED, school_day.getUsed());
values.put(DatabaseHelper.KEY_SSCHOOL_DAY_NO_OF_LESSONS, school_day.getNo_of_lessons());

// insert row
long sschool_day_id = db.insert(DatabaseHelper.TABLE_SSCHOOL_DAY, null, values);

return sschool_day_id;
}

/*
* getting all SchoolDays
*/
public List<SschoolDay_model_class> getAllSSchoolDays() {
Log.v("Peep", "BEFORE");
List<SschoolDay_model_class> school_day = new ArrayList<SschoolDay_model_class>();
Log.v("Peep", "after");
String selectQuery = "SELECT * FROM " + DatabaseHelper.TABLE_SSCHOOL_DAY;

Log.e(DatabaseHelper.LOG, selectQuery);

SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
SschoolDay_model_class sdy = new SschoolDay_model_class();
sdy.setSchool_day_id(c.getInt((c.getColumnIndex(DatabaseHelper.KEY_SSCHOOL_DAY_ID))));
sdy.setSchool_day(c.getString(c.getColumnIndex(DatabaseHelper.KEY_SSCHOOL_DAY)));
sdy.setUsed(c.getString(c.getColumnIndex(DatabaseHelper.KEY_SSCHOOL_DAY_USED)));
sdy.setNo_of_lessons(c.getInt(c.getColumnIndex(DatabaseHelper.KEY_SSCHOOL_DAY_NO_OF_LESSONS)));

// adding to SchoolDay list
school_day.add(sdy);
} while (c.moveToNext());

}
return school_day;
}

/*
* Updating a SchoolDay
*/
public int updatesSchoolDay(SschoolDay_model_class school_day) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(DatabaseHelper.KEY_SSCHOOL_DAY, school_day.getschool_day());
values.put(DatabaseHelper.KEY_SSCHOOL_DAY_USED, school_day.getUsed());
values.put(DatabaseHelper.KEY_SSCHOOL_DAY_NO_OF_LESSONS, school_day.getNo_of_lessons());
// updating row
return db.update(DatabaseHelper.TABLE_SSCHOOL_DAY, values, DatabaseHelper.KEY_SSCHOOL_DAY_ID + " = ?",
new String[] { String.valueOf(school_day.getSchool_day_id()) });
}
@Override
public void onCreate(SQLiteDatabase db) {


// creating required tables
db.execSQL(DatabaseHelper.CREATE_TABLE_SSCHOOL_DAY);
db.execSQL(DatabaseHelper.CREATE_TABLE_SPERIODS);
db.execSQL(DatabaseHelper.CREATE_TABLE_SLESSON);
db.execSQL(DatabaseHelper.CREATE_TABLE_SHOMEWORK);
db.execSQL(DatabaseHelper.CREATE_TABLE_SEXAM);
db.execSQL(DatabaseHelper.CREATE_TABLE_SLESSON_TIME);
//creating revision tables
db.execSQL(DatabaseHelper.CREATE_TABLE_RREVISION_DAY);
db.execSQL(DatabaseHelper.CREATE_TABLE_RSESSION_PERIOD);
db.execSQL(DatabaseHelper.CREATE_TABLE_RSESSION);
db.execSQL(DatabaseHelper.CREATE_TABLE_RSESSION_TIME);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " +DatabaseHelper.TABLE_SSCHOOL_DAY);
db.execSQL("DROP TABLE IF EXISTS " +DatabaseHelper.TABLE_SPERIODS);
db.execSQL("DROP TABLE IF EXISTS " +DatabaseHelper.TABLE_SLESSON);
db.execSQL("DROP TABLE IF EXISTS " +DatabaseHelper.TABLE_SHOMEWORK);
db.execSQL("DROP TABLE IF EXISTS " +DatabaseHelper.TABLE_SEXAM);
db.execSQL("DROP TABLE IF EXISTS " +DatabaseHelper.TABLE_SLESSON_TIME);

db.execSQL("DROP TABLE IF EXISTS " +DatabaseHelper.TABLE_RREVISION_DAY);
db.execSQL("DROP TABLE IF EXISTS " +DatabaseHelper.TABLE_RSESSION_PERIOD);
db.execSQL("DROP TABLE IF EXISTS " +DatabaseHelper.TABLE_RSESSION);
db.execSQL("DROP TABLE IF EXISTS " +DatabaseHelper.TABLE_RSESSION_TIME);
// create new tables
onCreate(db);
}
public SschoolDay_CRUD(Context context) {
super(context, DatabaseHelper.DATABASE_NAME, null, DatabaseHelper.DATABASE_VERSION);
// TODO Auto-generated constructor stub
}

编辑:新错误! (耶)现在我得到...考虑将其回滚到 1000+ 线,至少是有效的!

03-13 12:32:24.416: E/AndroidRuntime(2374): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference

因为这条线:
SQLiteDatabase db = this.getReadableDatabase();

这里是 DatabaseHelper 的构造函数:

    public Context DBHContext;

public DatabaseHelper(Context context) {


super(context, DATABASE_NAME, null, DATABASE_VERSION);
DBHContext = context;
}

然后我在 SschoolDay_CRUD 的初始化中使用 DBHContext

SschoolDay_CRUD sschool_day = new SschoolDay_CRUD(DBHContext);

最佳答案

从您的代码看来,此时您的 sschool_day 尚未初始化:

     s = sschool_day.getAllSSchoolDays();

在调用 getAllSSchoolDays(); 之前,你必须初始化 SschoolDay_CRUD sschool_day;

你的初始化应该是这样的:

    SschoolDay_CRUD sschool_day = new SschoolDay_CRUD(yourContext);
List<SschoolDay_model_class> s;
public List<SschoolDay_model_class> getAllSSchoolDays() {
Log.v("Does it get here?", "YES");

s = sschool_day.getAllSSchoolDays();
Log.v("Does it get here?", "YES2");

return s;
}

关于java - 甚至不进入方法。错误 "invoke virtual method java.lang.list on a null object reference",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29012681/

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