I need to perform a search operation into my SQLite DB.
In my Activity, I have EditText for entering the keyword and a search Button. Once the user clicks the button, the EditText text will be stored in the keyword and sent to a search(String keyword) method in the DBHelper class.
I have this code in my SearchActivityfor onCreate() and onClick():
//SearchActivity class extends ListActivity and implements OnClickListener
Button search;
DataBaseHelper myDbHelper;
SimpleCursorAdapter ca;
ListView lv;
EditText txt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_layout);
txt = (EditText) findViewById(R.id.search_term);
search = (Button) findViewById(R.id.search_button);
search.setOnClickListener(this);
}
public void onClick(View v) {
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {}
try {
String keyword= txt.getText().toString();
Cursor cursor = myDbHelper.search(keyword); //A method in my DB
cursor.moveToFirst();
String[] columns = {cursor.getColumnName(1)};
int[] columnsLayouts = {R.id.item_title};
ca = new SimpleCursorAdapter(this.getBaseContext(), R.layout.items_layout, cursor,columns , columnsLayouts);
lv = getListView();
lv.setAdapter(ca);
} catch(Exception e){}
}
However, I got NullPointerException in the LogCat!! I couldn’t find where the error is.
Anyone can help ?
The description to the NullPointerException could be a little more informative. But it is very likely that the NullPointerException occurs here:
or here:
Either in the first part in line 1: or 2: or in the second part in line 2:
You should always check if objects which rely on the existence of data are
not null. So you should check ifcursorisnull. Another thing could be that you never initializemyDbHelper.