I am showing the all available tables from my database in to list view. But if there are no any tables there then it will give the numm pointer Exception. The code is like below:
public ArrayList<Object> showAllTable()
{ ArrayList tableList = new ArrayList();
String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table'";
Cursor cursor = db.rawQuery(SQL_GET_ALL_TABLES, null);
//////////////////
if (cursor.moveToLast())
{
do
{
if(cursor.getString(0).equals("android_metadata"))
{
//System.out.println("Get Metadata");
continue;
}
else
{
tableList.add(cursor.getString(0));
}
}
while (cursor.moveToPrevious());
}
/////////////////////开发者_如何学Go//
cursor.close();
return tableList;
}
Please help me in this And the Exception i got is like:
09-09 09:57:25.873: ERROR/AndroidRuntime(574): FATAL EXCEPTION: main
09-09 09:57:25.873: ERROR/AndroidRuntime(574): android.database.sqlite.SQLiteException: near "null": syntax error: create table null (_id integer primary key, isbn text not null,title text not null,word text not null,sound text not null,publisher text not null); 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1727) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at com.quiz.spellingquiz.DBAdapter$DatabaseHelper.onCreate(DBAdapter.java:68) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at com.quiz.spellingquiz.DBAdapter.open(DBAdapter.java:88) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at com.quiz.spellingquiz.MenuPage$2.onClick(MenuPage.java:51) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at android.view.View.performClick(View.java:2408) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at android.view.View$PerformClick.run(View.java:8816) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at android.os.Handler.handleCallback(Handler.java:587) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at android.os.Handler.dispatchMessage(Handler.java:92) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at android.os.Looper.loop(Looper.java:123) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at android.app.ActivityThread.main(ActivityThread.java:4627) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at java.lang.reflect.Method.invokeNative(Native Method) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at java.lang.reflect.Method.invoke(Method.java:521) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 09-09 09:57:25.873: ERROR/AndroidRuntime(574): at dalvik.system.NativeStart.main(Native Method)
So, give me solution how to resolve it. . . Thanks.
The Line at 68 is:
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);
// below line is 68 db.execSQL("create table "+DATABASE_TABLE+" (_id integer primary key, " + "isbn text not null," + "title text not null," + "word text not null," + "sound text not null," + "publisher text not null);"); }
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion)
{
Log.w(TAG, "Upgrading database from version "+oldVersion+" to "+newVersion+", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
onCreate(db);
}
}
I think your DATABASE_TABLE variable is null and probably that is why you are getting null pointer exception.
based on this error
09-09 09:57:25.873: ERROR/AndroidRuntime(574): android.database.sqlite.SQLiteException: near "null": syntax error: create table null (_id integer primary key, isbn text not null,title text not null,word text not null,sound text not null,publisher text not null);
see the example code.
public class DBAdapter
{
private static final String DATABASE_NAME = "myDatabase.db";
private static final int DATABASE_VERSION = 1;
private static final String USERDETAILS=
"create table userdetails(usersno integer primary key autoincrement,userid text not null ,username text not null,password text not null,photo BLOB,visibility 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);
}
public void onCreate(SQLiteDatabase db)
{
db.execSQL(USERDETAILS);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS users");
onCreate(db);
}
}
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
public void close()
{
DBHelper.close();
}
}
精彩评论