我正在尝试检索我的数据库中的所有数据,但是在获取db的实例时获取空指针异常它返回null,我不知道它为什么会发生。我也发布了我的logcat,请检查一下并帮我解决这个问题......
DataBaseView.java
package com.vibrator;
import android.app.Activity;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class DataBaseView extends Activity{
DataBaseAdapter DataBaseAdapter;
ArrayList<String> al;
Cursor cursor;
DataBaseHelper dbHelper;
public SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.database_view);
System.out.println("entered in database");
ListView list1 =(ListView) findViewById(R.id.list);
al = new ArrayList<String>();
DataBaseAdapter=new DataBaseAdapter(this);
db = dbHelper.getWritableDatabase();
//DataBaseAdapter.open();
System.out.println("entered try");
cursor=db.rawQuery("select * from DETAILS", null);
if (cursor.moveToFirst()) {
do {
System.out.println("entered if loop");
String id =cursor.getString(cursor.getColumnIndex("ID"));
String name =cursor.getString(cursor.getColumnIndex("NAME"));
//String pattern =cursor.getString(cursor.getColumnIndex("PATTERN"));
//store the values in arraylist
al.add(id+". "+name);
}
while (cursor.moveToNext());
}
else{
System.out.println("cursor is null");
}
cursor.close();
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, al);
list1.setAdapter(adapter);
DataBaseAdapter.close();
}
}
logcat的:
08-06 12:41:14.124: I/System.out(10311): entered in database
08-06 12:41:14.184: D/AndroidRuntime(10311): Shutting down VM
08-06 12:41:14.184: W/dalvikvm(10311): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-06 12:41:14.294: E/AndroidRuntime(10311): FATAL EXCEPTION: main
08-06 12:41:14.294: E/AndroidRuntime(10311): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vibrator/com.vibrator.DataBaseView}: java.lang.NullPointerException
08-06 12:41:14.294: E/AndroidRuntime(10311): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-06 12:41:14.294: E/AndroidRuntime(10311): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-06 12:41:14.294: E/AndroidRuntime(10311): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-06 12:41:14.294: E/AndroidRuntime(10311): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-06 12:41:14.294: E/AndroidRuntime(10311): at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 12:41:14.294: E/AndroidRuntime(10311): at android.os.Looper.loop(Looper.java:123)
08-06 12:41:14.294: E/AndroidRuntime(10311): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-06 12:41:14.294: E/AndroidRuntime(10311): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 12:41:14.294: E/AndroidRuntime(10311): at java.lang.reflect.Method.invoke(Method.java:521)
08-06 12:41:14.294: E/AndroidRuntime(10311): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-06 12:41:14.294: E/AndroidRuntime(10311): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-06 12:41:14.294: E/AndroidRuntime(10311): at dalvik.system.NativeStart.main(Native Method)
08-06 12:41:14.294: E/AndroidRuntime(10311): Caused by: java.lang.NullPointerException
08-06 12:41:14.294: E/AndroidRuntime(10311): at com.vibrator.DataBaseView.onCreate(DataBaseView.java:35)
08-06 12:41:14.294: E/AndroidRuntime(10311): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-06 12:41:14.294: E/AndroidRuntime(10311): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-06 12:41:14.294: E/AndroidRuntime(10311): ... 11 more
DataBaseAdapter.java
package com.vibrator;
//import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
//import android.widget.ArrayAdapter;
import android.widget.Toast;
@SuppressLint("ShowToast")
public class DataBaseAdapter {
static final String DATABASE_NAME = "Vibrator.raj.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table "+"DETAILS"+
"( " +"ID"+" integer primary key autoincrement,"+ ""
+ "NAME text,"
+ "PATTERN text); ";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public DataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Method to openthe Database
public DataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public DataBaseAdapter openr() throws SQLException
{
db = dbHelper.getReadableDatabase();
return this;
}
// Method to close the Database
public void close()
{
db.close();
}
// method returns an Instance of the Database
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
}
答案 0 :(得分:0)
看起来您的dbHelper从未在DataBaseView中初始化,因此为null。