Attempt to invoke virtual method android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' null Object Reference

Asked

Viewed 641 times

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.

  • Important information is missing in your code to confirm a response, for example this bsdb. that is not shown where it is instantiated.

  • Which query is wrong?

  • the second query that is giving errors. The bsdb. is instantiated at the beginning of the code: public class LivroDAO2 {&#xA; private SQLiteDatabase db;&#xA; private BookSwapOpenHelper bsdb;&#xA; public LivroDAO2(Context c) {&#xA; bsdb = new BookSwapOpenHelper(c);&#xA; }&#xA; public void abrir() {&#xA; db = bsdb.getWritableDatabase();&#xA; }&#xA; public void fechar() {&#xA; db.close();&#xA; }&#xA;

1 answer

-2

Before using the cursor, try placing a check like this (a way around the error, but does not resolve it)

if(cursor != null && cursor.moveToFirst())

If it keeps going wrong, I think it has to do with the ";" you have at the end of the query.

I hope I’ve helped!

  • That doesn’t solve the mistake just circumvents it.

  • Because, his problem is in the query he uses no? the data may not exist in DB so it returns null. I will edit my answer as a way around the error

  • The cursor is not supposed to be null. If there is no data satisfying the query an empty cursor is returned(moveToFirst()returns false),

  • exactly! the problem is in the query

  • What’s wrong with the query? If, as the AP says, the first one works, why doesn’t the second one?

  • I’m not sure what caused the problem, but I believe it was the query structure. I did a little research on how Rawquery works and wrote that way: Cursor Cur = db.rawQuery("SELECT * FROM uBooks WHERE trocavel=?;",new String[]{"Indisponível"}); Now the list loads the data.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.