0
Okay, I’m new to Android and I’m developing a somewhat complex application for a course completion. At the moment the problem is to display a listview with information from a local database (Sqlite). I would be calmer if it were not for the fact that I already made a listview in the same format that worked, and that did not. Below the error, followed by Logcat.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.banhammer.bookswap, PID: 30658
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.banhammer.bookswap/com.banhammer.bookswap.Activities.EstanteActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference
at com.banhammer.bookswap.DAO.LivroDAO2.showEstante(LivroDAO2.java:67)
at com.banhammer.bookswap.Activities.EstanteActivity.onCreate(EstanteActivity.java:31)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
I am trying to load the list with the database data through a very simple DAO. The first method works and the second does not:
public ArrayList<Livro> procurarLivro(String parametro) {
bsdb.getWritableDatabase();
ArrayList<Livro> listL = new ArrayList<Livro>();
Cursor Cr = db.rawQuery("select * from uBooks where titulo='"+parametro+"' and trocavel='Disponível';",null);
while (Cr.moveToNext()) {
Livro l = new Livro();
l.setTitulo(Cr.getString(1));
l.setAutor(Cr.getString(2));
l.setGenero1(Cr.getString(3));
l.setGenero2(Cr.getString(4));
listL.add(l);
}
db.close();
Cr.close();
return listL;
}
public ArrayList<Livro> showEstante() {
bsdb.getWritableDatabase();
ArrayList<Livro> listL = new ArrayList<Livro>();
Cursor Cr = db.rawQuery("select * from uBooks;",null);
while (Cr.moveToNext()) {
Livro l = new Livro();
l.setTitulo(Cr.getString(1));
l.setAutor(Cr.getString(2));
l.setGenero1(Cr.getString(3));
l.setGenero2(Cr.getString(4));
listL.add(l);
}
db.close();
Cr.close();
return listL;
}
This one is the Activity that gives problem. It doesn’t even initialize because the list is being generated in creation.
package com.banhammer.bookswap.Activities;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import com.banhammer.bookswap.DAO.Livro;
import com.banhammer.bookswap.DAO.LivroDAO2;
import com.banhammer.bookswap.R;
import java.util.List;
public class EstanteActivity extends ListActivity {
private LivroDAO2 LDAO2;
ImageButton home;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_estante);
LDAO2=new LivroDAO2(this);
home = (ImageButton)findViewById(R.id.cad_home);
List<Livro> estante=LDAO2.showEstante();
ArrayAdapter<Livro> adapter=new ArrayAdapter<Livro>(this,android.R.layout.simple_list_item_1,estante);
this.setListAdapter(adapter);
this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
public void estHome(View v) {
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
@Override
protected void onPause() {
LDAO2.fechar();
super.onPause();
}
}
If anyone has a solution or an idea would be very welcome. I tried everything I knew and nothing to fix it.
The error is by Cursor
cr
be null. If the first method(searchLivro) works I can’t find reason why the other one won’t work.– ramaral
Important information is missing in your code to confirm a response, for example this
bsdb.
that is not shown where it is instantiated.– viana
Which query is wrong?
– ramaral
the second query that is giving errors. The
bsdb.
is instantiated at the beginning of the code:public class LivroDAO2 {
 private SQLiteDatabase db;
 private BookSwapOpenHelper bsdb;
 public LivroDAO2(Context c) {
 bsdb = new BookSwapOpenHelper(c);
 }
 public void abrir() {
 db = bsdb.getWritableDatabase();
 }
 public void fechar() {
 db.close();
 }

– Jhonatan 'Spark' Rafael