0

我正在尝试使用 Sqlite 插入值,但是在插入表时,它会抛出 nullpointerException。

这是我的数据库类:

public class FeedEntry extends SQLiteOpenHelper implements BaseColumns{

public static final String Table_Name = "Capture";
public static final String ColumnName_EmpCode = "EmpCode";
public static final String ColumnName_CallName = "CallName";
public static final String ColumnName_FirstName = "FirstName";
public static final String columnName_LastName = "LastName";

private static final String DATABASE_NAME = "Capture.db";
private static final int DATABASE_VERSION = 1;


private static final String DATABASE_CREATE = " create table "+Table_Name+"( "+ColumnName_EmpCode+" text, "
        +ColumnName_CallName+" text);";// "+COLUMN_NAME_PASSWORD+ " text );";


public FeedEntry(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(DATABASE_CREATE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS " + Table_Name);
    onCreate(db);

}

}

这是我的 DAO 课程:

public class CaptureDAO {

private static SQLiteDatabase dataBase;
private FeedEntry dbHelper = null;
private String[] allColumns = {FeedEntry.ColumnName_EmpCode, FeedEntry.ColumnName_CallName};

public CaptureDAO(Context context) {
    dbHelper = new FeedEntry(context);
}

public void open() throws SQLException {
    dataBase = dbHelper.getWritableDatabase();
}

public void close() {
    dbHelper.close();
}

public static void empDetails(String empCode, String callName){
    System.out.println("IN DAO CLASS "+empCode+" "+callName);
    ContentValues values = new ContentValues();
    System.out.println("In dao step 1");
    values.put(FeedEntry.ColumnName_EmpCode, empCode);
    System.out.println("In dao step 2");
    values.put(FeedEntry.ColumnName_CallName, callName);
    System.out.println("In dao step 3");

    long insertId = dataBase.insert(FeedEntry.Table_Name, null, values);
    System.out.println("InserId "+insertId);
    //Cursor cursor = dataBase.query(FeedEntry.Table_Name, allColumns, FeedEntry.ColumnName_EmpCode+" = "+insertId, null, null, null, null);

}

}

我正在尝试在活动类中使用以下行插入值:

CaptureDAO.empDetails(empCode.getText().toString(), empName.getText().toString());

这是我的 LogCat:

02-13 05:42:59.295: E/AndroidRuntime(934): FATAL EXCEPTION: main
02-13 05:42:59.295: E/AndroidRuntime(934): java.lang.NullPointerException
02-13 05:42:59.295: E/AndroidRuntime(934):  at 

com.virinchi.capture.dao.CaptureDAO.empDetails(CaptureDAO.java:39)
02-13 05:42:59.295: E/AndroidRuntime(934):  at com.virinchi.capture.captureDirectory.CaptureActivity.showEmployees(CaptureActivity.java:139)
02-13 05:42:59.295: E/AndroidRuntime(934):  at com.virinchi.capture.bo.async.FetchEmployeeAsyncTask.onPostExecute(FetchEmployeeAsyncTask.java:101)
02-13 05:42:59.295: E/AndroidRuntime(934):  at com.virinchi.capture.bo.async.FetchEmployeeAsyncTask.onPostExecute(FetchEmployeeAsyncTask.java:1)
02-13 05:42:59.295: E/AndroidRuntime(934):  at android.os.AsyncTask.finish(AsyncTask.java:631)
02-13 05:42:59.295: E/AndroidRuntime(934):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-13 05:42:59.295: E/AndroidRuntime(934):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-13 05:42:59.295: E/AndroidRuntime(934):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 05:42:59.295: E/AndroidRuntime(934):  at android.os.Looper.loop(Looper.java:137)
02-13 05:42:59.295: E/AndroidRuntime(934):  at android.app.ActivityThread.main(ActivityThread.java:5039)
02-13 05:42:59.295: E/AndroidRuntime(934):  at java.lang.reflect.Method.invokeNative(Native Method)
02-13 05:42:59.295: E/AndroidRuntime(934):  at java.lang.reflect.Method.invoke(Method.java:511)
02-13 05:42:59.295: E/AndroidRuntime(934):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-13 05:42:59.295: E/AndroidRuntime(934):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-13 05:42:59.295: E/AndroidRuntime(934):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

2

您需要将当前上下文传递FeedEntry给创建数据库。将上下文传递给CaptureDAO使用参数化构造函数并staticempDetails方法中删除:

CaptureDAO CaptureDAOObj=new CaptureDAO(CaptureActivity.this);//<<pass context here
CaptureDAOObj.empDetails(empCode.getText().toString(),
                                     empName.getText().toString());
于 2013-02-13T06:05:05.360 回答
0

数据库为空。在使用插入语句之前尝试对其进行初始化。

将构造函数更改为 -

public CaptureDAO(Context context) {
    dbHelper = new FeedEntry(context);
    dataBase = dbHelper.getWritableDatabase(); // initialize ur db here
}

在活动使用中 -

    CaptureDAO captureDao = new CaptureDAO(getApplicationContext());
    captureDao.empDetails("ËmpCode", "empName");

希望这可以帮助。

于 2013-02-13T06:08:18.697 回答