4

I have my class DAO, and when i call my class conexao the method return null, but why?

public void criarBanco() {
        Conexao conexao = new Conexao();
        try {
            BANCO_DE_DADOS = conexao.abrirBanco(); // <<<<<<<<<<<<<
            String SQL = "CREATE TABLE IF NOT EXISTS tablecadastropessoa ( _id INTEGER PRIMARY KEY, nome TEXT, telefone TEXT)";
            BANCO_DE_DADOS.execSQL(SQL);
        } catch (Exception e) {
            Log.i(TAG, "Erro ao criar o banco" + e);
        } finally {
            //conexao.fecharBanco(BANCO_DE_DADOS);
        }
    }

    // ABRIR BANCO
    public SQLiteDatabase abrirBanco(){
        try {
            BANCO_DE_DADOS = openOrCreateDatabase(NOME_BANCO, MODE_PRIVATE, null); // RETURN NULL
        } catch (Exception e) {
            Util.exibirMensagem("Erro ao criar o banco: " + e, AulaCadastroActivity.context);
            Log.i(TAG, "Erro ao abrir o banco: " + e);
        }
        return BANCO_DE_DADOS;
    }

ERROR IS

07-05 13:12:54.800: E/AndroidRuntime(223): Uncaught handler: thread main exiting due to uncaught exception
07-05 13:12:54.811: E/AndroidRuntime(223): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.cadastro/br.cadastro.AulaCadastroActivity}: java.lang.NullPointerException
07-05 13:12:54.811: E/AndroidRuntime(223):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
07-05 13:12:54.811: E/AndroidRuntime(223):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
07-05 13:12:54.811: E/AndroidRuntime(223):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
07-05 13:12:54.811: E/AndroidRuntime(223):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
07-05 13:12:54.811: E/AndroidRuntime(223):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 13:12:54.811: E/AndroidRuntime(223):  at android.os.Looper.loop(Looper.java:123)
07-05 13:12:54.811: E/AndroidRuntime(223):  at android.app.ActivityThread.main(ActivityThread.java:4363)
07-05 13:12:54.811: E/AndroidRuntime(223):  at java.lang.reflect.Method.invokeNative(Native Method)
07-05 13:12:54.811: E/AndroidRuntime(223):  at java.lang.reflect.Method.invoke(Method.java:521)
07-05 13:12:54.811: E/AndroidRuntime(223):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-05 13:12:54.811: E/AndroidRuntime(223):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-05 13:12:54.811: E/AndroidRuntime(223):  at dalvik.system.NativeStart.main(Native Method)
07-05 13:12:54.811: E/AndroidRuntime(223): Caused by: java.lang.NullPointerException
07-05 13:12:54.811: E/AndroidRuntime(223):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
07-05 13:12:54.811: E/AndroidRuntime(223):  at br.cadastro.Conexao.abrirBanco(Conexao.java:20)
07-05 13:12:54.811: E/AndroidRuntime(223):  at br.cadastro.ContadosDAO.criarBanco(ContadosDAO.java:27)
07-05 13:12:54.811: E/AndroidRuntime(223):  at br.cadastro.AulaCadastroActivity.inicializar(AulaCadastroActivity.java:81)
07-05 13:12:54.811: E/AndroidRuntime(223):  at br.cadastro.AulaCadastroActivity.onCreate(AulaCadastroActivity.java:27)
07-05 13:12:54.811: E/AndroidRuntime(223):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-05 13:12:54.811: E/AndroidRuntime(223):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
07-05 13:12:54.811: E/AndroidRuntime(223):  ... 11 more
duggu
  • 37,851
  • 12
  • 116
  • 113
SHENN17
  • 41
  • 1
  • 5
  • What is the full error message? Specifically, what is the stack trace? – ikh Jul 05 '12 at 13:07
  • the error is when i open/creat mey bd 'openOrCreateDatabase(NOME_BANCO, MODE_PRIVATE, null); // RETURN NULL' – SHENN17 Jul 05 '12 at 13:08
  • The part of the error that says "Caused by: java.lang.NullPointerException ... at android.content.ContextWrapper.openOrCreateDatabase" suggests that you may be passing a `null` to `openOrCrateDatabase` where there shouldn't be null. Can you confirm that `NOME_BANCO` is not null immediately before it is passed to `openOrCreateDatabase`? – ikh Jul 05 '12 at 13:19
  • NOME_BANCO is not null before it is passed to openOrCreateDatabase =D – SHENN17 Jul 05 '12 at 13:55

1 Answers1

1

It is likely that NOME_BANCO is not initialised in this line

BANCO_DE_DADOS = openOrCreateDatabase(NOME_BANCO, MODE_PRIVATE, null);

Which means you are in reality passing:

BANCO_DE_DADOS = openOrCreateDatabase(null, MODE_PRIVATE, null);

Hence the null Pointer exception. Please check it is set to something, and that it is a valid path.

EDIT:

Change the file name to have the correct extension. .db

private static String NOME_BANCO = "Cadastro";

Becomes:

private static String NOME_BANCO = "Cadastro.db";
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • sorry i dont put this.. 'private static final String TAG = "Conexao"; private static String NOME_BANCO = "Cadastro"; private Context context; SQLiteDatabase BANCO_DE_DADOS = null;' – SHENN17 Jul 05 '12 at 13:23
  • See Edit :) you are using an invalid path/name – IAmGroot Jul 05 '12 at 13:29
  • i dont understend invalid path/name? – SHENN17 Jul 05 '12 at 13:30
  • You need to specify the Database name. This is the name it creates on your storage device. A database requires the `.db` extension – IAmGroot Jul 05 '12 at 13:31
  • Doomsknight not work this private static String NOME_BANCO = "Cadastro.db"; As I put the code formatted? – SHENN17 Jul 05 '12 at 13:34
  • Doomsknight i cant remove the final null =/ – SHENN17 Jul 05 '12 at 13:40
  • The last thing that will work is `BANCO_DE_DADOS = openOrCreateDatabase( NOME_BANCO, MODE_PRIVATE, SQLiteDatabase.CREATE_IF_NECESSARY);` Else Ive no idea sorry – IAmGroot Jul 05 '12 at 13:44