gpt4 book ai didi

java - Android、SQL、空指针异常

转载 作者:行者123 更新时间:2023-12-02 07:44:56 24 4
gpt4 key购买 nike

我正在编写 Android、sql atm 代码,我需要你的帮助。无法启动应用程序,因为我有一个空指针,并且找不到它......

所以我尝试阅读很多内容,但我认为我并没有清楚地理解这里的所有内容。因此,如果您能尝试告诉我出了什么问题,那就太好了。

你能帮我一下吗?

感谢大家阅读本文! :)

>public class DBAdapter
>{
> //Variables and so on..
> public static final String KEY_ROWID = "_id";
> public static final String KEY_ISBN = "isbn";
> public static final String KEY_TITLE = "title";
> public static final String KEY_PUBLISHER ="publisher";
> private static final String TAG ="DBAdapter";
> private static final String DATABASE_NAME = "books";
> private static final String DATABASE_TABLE = "titles";
> private static final int DATABASE_VERSION = 1;
> private static final String DATABASE_CREATE = "create table titles (_id integer >primary key autoincrement, "+ "isbn text not null, title text not null, "+ "publisher >text not null);";
>
> private final Context context;
>
> private DatabaseHelper DBHelper;
> private SQLiteDatabase db;
>
> public DBAdapter(Context ctx)
> {
> this.context = ctx;
> DBHelper = new DatabaseHelper(context);
> }
>
> private static class DatabaseHelper extends SQLiteOpenHelper
> {
> DatabaseHelper(Context context)
> {
> super(context, DATABASE_NAME, null, DATABASE_VERSION);
> }
> @Override
> public void onCreate(SQLiteDatabase db)
> {
> db.execSQL(DATABASE_CREATE);
> }
> @Override
> public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
> {
> Log.w(TAG, "Upgrading database from version "+oldVersion+" to >"+newVersion+" which will destory all old data");
> db.execSQL("DROP TABLE IF EXISTS titles");
> onCreate(db);
> }
> }
>
> //opens database
> public DBAdapter open() throws SQLException
> {
> db = DBHelper.getWritableDatabase();
> return this;
> }
>
> //closes database
> public void close()
> {
> DBHelper.close();
> }
>
> //insert title to database
> ContentValues initialValues = new ContentValues();
> public long insertTitle(String isbn, String title, String publisher)
> {
> initialValues.put(KEY_ISBN, isbn);
> initialValues.put(KEY_TITLE, title);
> initialValues.put(KEY_PUBLISHER, publisher);
> return db.insert(DATABASE_TABLE, null, initialValues);
> }
>
> //Delete specific title
> public boolean deleteTitle(long rowId)
> {
> return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
> }
>
> //Retrieves all titles
> public Cursor getAllTitles()
> {
> return db.query(DATABASE_TABLE, new String[]
> {KEY_ROWID, KEY_ISBN, KEY_TITLE, KEY_PUBLISHER}, null, null, null, null, >null);
> }
>
> //Retrieves a particular title
> public Cursor getTitle(Long rowId) throws SQLException
> {
> Cursor mCursor = db.query(true, DATABASE_TABLE, new String[]
> {KEY_ROWID, KEY_ISBN, KEY_TITLE, KEY_PUBLISHER}, KEY_ROWID >+ "=" + rowId, null, null, null, null, null);
> if(mCursor != null)
> {
> mCursor.moveToFirst();
> }
> return mCursor;
> }
>
> //Update a title
> ContentValues args = new ContentValues();
> public boolean updateTitle(long rowId, String title, String isbn, String publisher)
> {
> args.put(KEY_ISBN, isbn);
> args.put(KEY_TITLE, title);
> args.put(KEY_PUBLISHER, publisher);
> return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
> }
>}

编辑:

这是我使用 DBAdapter 类的代码:

public class DatabasActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

DBAdapter db = new DBAdapter(this);

//Add 2 titles
db.open();
long id;
id = db.insertTitle("0470285818", "C# 2008 Programmer's Reference", "Wrox");
id = db.insertTitle("047017661X", "Professional Windows Vista Gadgets Programming", "Wrox");
db.close();

//Get all titles
db.open();
Cursor c = db.getAllTitles();
if(c.moveToFirst())
{
do
{
DisplayTitle(c);
}
while(c.moveToNext());
{

}
}
db.close();

//get a title
db.open();
Cursor c1 = db.getTitle((long) 2);
if(c1.moveToFirst())
{
DisplayTitle(c1);
}
else
{
Toast.makeText(this, "No title found", Toast.LENGTH_LONG).show();
}
db.close();

//Update a title
db.open();
if(db.updateTitle(1, "0470285818", "C# 2008 Programmer's Reference", "Wrox Press"))
{
Toast.makeText(this, "Update successful", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "Update failed", Toast.LENGTH_LONG).show();
}
db.close();
//Retrive the updated title
Cursor c2 = db.getTitle((long) 1);
if(c.moveToFirst())
{
DisplayTitle(c2);
}
else
{
Toast.makeText(this, "No title found", Toast.LENGTH_LONG).show();
}
}

public void DisplayTitle(Cursor c)
{
Toast.makeText(this, "id: "+c.getString(0)+"\n"+"ISBN: "+c.getString(1)+"\n"+"Title: "+c.getString(2)+"\n"+"Publisher: "+c.getString(3), Toast.LENGTH_LONG).show();
}

}

我设法从 logcat 中获得一些输出,如下:

06-14 08:43:00.882: D/AndroidRuntime(426): Shutting down VM
06-14 08:43:00.892: W/dalvikvm(426): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-14 08:43:00.912: E/AndroidRuntime(426): FATAL EXCEPTION: main
06-14 08:43:00.912: E/AndroidRuntime(426): java.lang.RuntimeException: Unable to start activity ComponentInfo{databas.test.org/databas.test.org.DatabasActivity}: java.lang.NullPointerException
06-14 08:43:00.912: E/AndroidRuntime(426): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-14 08:43:00.912: E/AndroidRuntime(426): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-14 08:43:00.912: E/AndroidRuntime(426): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-14 08:43:00.912: E/AndroidRuntime(426): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-14 08:43:00.912: E/AndroidRuntime(426): at android.os.Handler.dispatchMessage(Handler.java:99)
06-14 08:43:00.912: E/AndroidRuntime(426): at android.os.Looper.loop(Looper.java:123)
06-14 08:43:00.912: E/AndroidRuntime(426): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-14 08:43:00.912: E/AndroidRuntime(426): at java.lang.reflect.Method.invokeNative(Native Method)
06-14 08:43:00.912: E/AndroidRuntime(426): at java.lang.reflect.Method.invoke(Method.java:507)
06-14 08:43:00.912: E/AndroidRuntime(426): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-14 08:43:00.912: E/AndroidRuntime(426): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-14 08:43:00.912: E/AndroidRuntime(426): at dalvik.system.NativeStart.main(Native Method)
06-14 08:43:00.912: E/AndroidRuntime(426): Caused by: java.lang.NullPointerException
06-14 08:43:00.912: E/AndroidRuntime(426): at databas.test.org.DBAdapter.insertTitle(DBAdapter.java:74)
06-14 08:43:00.912: E/AndroidRuntime(426): at databas.test.org.DatabasActivity.onCreate(DatabasActivity.java:24)
06-14 08:43:00.912: E/AndroidRuntime(426): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-14 08:43:00.912: E/AndroidRuntime(426): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-14 08:43:00.912: E/AndroidRuntime(426): ... 11 more

最佳答案

我看到的唯一潜在的 NPE 是在 DbAdapter 中未调用 open()

关于java - Android、SQL、空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11028976/

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