Ok, 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'd be calmer if it were not for the fact that I already did a listview in the same format that worked, and that one 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'm trying to load the list with the bank's 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 is the activity that gives problem. It does not even initialize because the list is being generated at 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 someone has a solution or an idea it would be very welcome. I tried everything I knew and did not fix it.