我知道这个问题被问过一千次,我读了很多这些问题和答案。但在这种情况下,我无法解决我的问题。 当我在我的Android应用程序中调用getWritableDatabase()函数时,我得到了这个例外:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.schneider_schwarz_inc.tagesablauf_log, PID: 12634
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.schneider_schwarz_inc.tagesablauf_log/de.schneider_schwarz_inc.tagesablauf_log.GUI.ToDo.ToDoList}: java.lang.RuntimeException: 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
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.RuntimeException: 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
at de.schneider_schwarz_inc.tagesablauf_log.Database.ToDo.ToDoDao.insertToDo(ToDoDao.java:41)
at de.schneider_schwarz_inc.tagesablauf_log.Todo.createTodo(Todo.java:25)
at de.schneider_schwarz_inc.tagesablauf_log.GUI.ToDo.ToDoList.onCreate(ToDoList.java:47)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
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
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at de.schneider_schwarz_inc.tagesablauf_log.Database.ToDo.ToDoDao.insertToDo(ToDoDao.java:32)
at de.schneider_schwarz_inc.tagesablauf_log.Todo.createTodo(Todo.java:25)
at de.schneider_schwarz_inc.tagesablauf_log.GUI.ToDo.ToDoList.onCreate(ToDoList.java:47)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
我的Sqliteopenhelper类:
public ToDoDbHelper(Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
System.out.println("creating database");
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public static int getDatabaseVersion() {
return DATABASE_VERSION;
}
我在这堂课中使用它:
public class ToDoDao {
private Context context;
private ToDoDbHelper toDoDbHelper;
public ToDoDao(Context context){
this.context = context;
toDoDbHelper = new ToDoDbHelper(context);
}
/**
* Inserts ToDos into Database
* @param data data to insert
* @return is -1 when error, else the id
*/
public long insertToDo(ToDoData data){
try{
SQLiteDatabase db = toDoDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ToDoStructure.ToDoEntry.COLUMN_NAME_TITLE, data.getTitle());
values.put(ToDoStructure.ToDoEntry.COLUMN_NAME_DATE, data.getDate());
return db.insert(ToDoStructure.ToDoEntry.TABLE_NAME, null, values);
}catch (Exception e){
System.out.println("ERROR!");
//TODO handling exception
throw new RuntimeException(e);
}
}
我在这里使用的道:
public class Todo {
private List<ToDoData> dataList;
private Context context;
private ToDoDao dao = new ToDoDao(context);
public Todo(Context context){
this.context = context;
//dataList = dao.getAllToDos();
dataList = new ArrayList<>();
}
public int createTodo(String title, String date){
ToDoData newData = new ToDoData(title, date);
long id = dao.insertToDo(newData);
if(id == -1){
return (int) id;
}
newData.setId(String.valueOf(id));
dataList.add(newData);
return 0;
}
至少我的活动在哪里获得我的背景:
public class ToDoList extends Activity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState){
System.out.println("createListView");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
Todo todo = new Todo(this);
todo.createTodo("Test1", "20160910");
List<ToDoData> todoList = todo.getDataList();
System.out.println(todoList.get(0).getTitle());
mAdapter = new ToDoViewAdapter(getBaseContext(), todoList);
mRecyclerView.setAdapter(mAdapter);
}
我认为问题在于我的背景。但我不知道要解决它......
谢谢!
答案 0 :(得分:0)
好的......我遇到了问题。
CASE
WHEN (
SELECT
COUNT(*)
FROM
my_table ) <= 0 THEN
'the rest OF the script here'
ELSE
'ALERT: Make sure there is nothing left in my_table'
END
当我将它传递给dao时,上下文为null。 相反,我现在已经这样做了:
private Context context;
private ToDoDao dao = new ToDoDao(context);
public Todo(Context context){
this.context = context;
//dataList = dao.getAllToDos();
dataList = new ArrayList<>();
}
这样的责备......