我正在尝试构建基于数据库的应用。但是当我按下搜索按钮时,应用程序意外停止。现在我能做些什么...
我的logcat消息是:
12-09 18:05:52.177 1552-1552/com.example.bank E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.bank.search$1.onClick(search.java:44)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
,search.java是:
public class search extends AppCompatActivity {
DatabaseOperations myDB;
Button search;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
search = (Button) findViewById(R.id.btn_search);
all_data();
}
public void showMessage(String title, String message)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void all_data()
{
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor res = myDB.getAllData();
if (res.getCount() == 0)
{
//message
showMessage("ERROR", "Nothig is Found");
return;
}
else
{
StringBuffer buffer = new StringBuffer();
while (res.moveToNext())
{
buffer.append("EMAIL : "+res.getString(0)+"\n");
buffer.append("FULL_NAME : "+res.getString(1)+"\n\n");
}
//show all data
showMessage("Data", buffer.toString());
}
}
});
}
}
DataBaseOperations.java:
public class DatabaseOperations extends SQLiteOpenHelper{
public static final int database_version = 1;
public String CREATE_QUERY = "CREATE TABLE " + TableData.TableInfo.TABLE_NAME+"("+ TableData.TableInfo.EMAIL+" TEXT,"+ TableData.TableInfo.FULL_NAME+" TEXT,"+ TableData.TableInfo.USER_NAME+" TEXT,"+ TableData.TableInfo.USER_PASS+" TEXT,"+ TableData.TableInfo.DISTRICT+" TEXT,"+ TableData.TableInfo.CITY+" TEXT,"+ TableData.TableInfo.VARSITY+" TEXT,"+ TableData.TableInfo.BLOOD_GROUP+" TEXT,"+ TableData.TableInfo.LAST_DATE+" TEXT,"+ TableData.TableInfo.PHONE_NUM+" TEXT );" ;
public DatabaseOperations(Context context) {
/*
some change is made here
check it for problem...
*/
super(context, TableData.TableInfo.DATABASE_NAME, null, database_version);
Log.d("Database operations", "Database Created");
}
@Override
public void onCreate(SQLiteDatabase sdb) {
sdb.execSQL(CREATE_QUERY);
Log.d("Database operations", "Table Created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void putInformation(DatabaseOperations dop, String email, String full_name, String user_name, String user_pass, String district, String city, String varsity, String blood_group, String last_date, String ph_num)
{
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.EMAIL, email);
cv.put(TableData.TableInfo.FULL_NAME, full_name);
cv.put(TableData.TableInfo.USER_NAME, user_name);
cv.put(TableData.TableInfo.USER_PASS, user_pass);
cv.put(TableData.TableInfo.DISTRICT, district);
cv.put(TableData.TableInfo.CITY, city);
cv.put(TableData.TableInfo.VARSITY, varsity);
cv.put(TableData.TableInfo.BLOOD_GROUP, blood_group);
cv.put(TableData.TableInfo.LAST_DATE, last_date);
cv.put(TableData.TableInfo.PHONE_NUM, ph_num);
long k = SQ.insert(TableData.TableInfo.TABLE_NAME, null, cv);
Log.d("Database operations", "One Raw Inserted");
}
public Cursor getInformation(DatabaseOperations dop)
{
SQLiteDatabase SQ = dop.getReadableDatabase();
String[] columns = {TableData.TableInfo.USER_NAME, TableData.TableInfo.USER_PASS};
Cursor CR = SQ.query(TableData.TableInfo.TABLE_NAME, columns, null, null, null, null, null);
return CR;
}
public Cursor getAllData()
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("Select * from "+TableData.TableInfo.TABLE_NAME, null);
return res;
}
}
帮我解决这个问题。 提前谢谢
答案 0 :(得分:3)
在您提供的代码中,myDB
永远不会被初始化。
因此Cursor res = myDB.getAllData();
会产生异常。