I am getting a null pointer exception when I have the following class:
public Cursor getAllData(String s) {
SQLiteDatabase db;
db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT nome FROM " + TABELA_RECEITAS, null);
if (res != null) {
res.moveToFirst();
}
return res;
}
I have also the following class that calls the first class:
if (s != null) {
Cursor cursor = myDB.getAllData(s);
Null Pointer Exception:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.projeto.asminhasreceitas, PID: 8263
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.projeto.asminhasreceitas.helper.DBHelper.getAllData(java.lang.String)' on a null object reference
at com.projeto.asminhasreceitas.ui.pesquisar.PesquisarFragment$1.onQueryTextSubmit(PesquisarFragment.java:63)
at android.widget.SearchView.onSubmitQuery(SearchView.java:1570)
at android.widget.SearchView.access$1000(SearchView.java:112)
at android.widget.SearchView$7.onEditorAction(SearchView.java:1542)
at android.widget.TextView.onEditorAction(TextView.java:7718)
at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:363)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:93)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
I/Process: Sending signal. PID: 8263 SIG: 9
What I am doing wrong?