Error of Nullpointerexception

Asked

Viewed 65 times

2

Caused by: java.lang.Nullpointerexception AT dao.Database.verificaSeTemRegistroNaTable(Database.java:184)

That error was presented in mine logcat.

The method:

public boolean verificaSeTemRegistroNaTabela(String tabela){
    String sqlSelect = "SELECT * FROM '" + tabela + "'";
    Cursor res = db.rawQuery(sqlSelect, null);
    if (res.getCount() > 0){
        return true;
    }
    return false;
}

Line with error:

Cursor res = db.rawQuery(sqlSelect, null);
  • 1

    Check if it’s not the db that ta null, as you are calling this method check?

  • What is the type of db. Are you sure it’s not null?

  • 1

    Read this: injection of SQL.

  • 1

    remove the single quotes String sqlSelect = "SELECT * FROM " + tabela; and check the variable db

1 answer

1

As quoted in the comments by Virgilio, your mistake is in passing the table with single quotes.

Take the value of the variable sqlSelect and run in your database, error will occur.

Pass parameters to sql commands always passing by bind variables, will make your code safer and simple to maintain in the future.

Browser other questions tagged

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