2012-07-05 187 views
4

我有我的類DAO,當我打電話給我的類conexao該方法返回null,但爲什麼?Android - openOrCreateDatabase - 空指針異常

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; 
    } 

誤差

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 
+0

什麼是完整的錯誤消息?具體來說,什麼是堆棧跟蹤? – ikh 2012-07-05 13:07:47

+0

錯誤是當我打開/創建mey bd 'openOrCreateDatabase(NOME_BANCO,MODE_PRIVATE,null); // RETURN NULL' – SHENN17 2012-07-05 13:08:53

+0

在「android.content.ContextWrapper.openOrCreateDatabase」中引發「由java.lang.NullPointerException ...引發的錯誤」部分表明,您可能正在將'null'傳遞給'openOrCrateDatabase',在那裏不應該爲空。你可以確認在傳遞給'openOrCreateDatabase'之前'NOME_BANCO'不是null嗎? – ikh 2012-07-05 13:19:17

回答

1

很可能NOME_BANCO沒有在這一行

BANCO_DE_DADOS = openOrCreateDatabase(NOME_BANCO, MODE_PRIVATE, null); 

這意味着你在現實中通過初始化:

BANCO_DE_DADOS = openOrCreateDatabase(null, MODE_PRIVATE, null); 

因此空指針除外離子。請檢查它是否設置了某些內容,並且這是一條有效的路徑。

編輯:

更改文件名具有正確的擴展名。 .db

private static String NOME_BANCO = "Cadastro"; 

變爲:

private static String NOME_BANCO = "Cadastro.db"; 
+0

之前不爲空。對不起,我不把這個.. '私人靜態最終字符串標記=「Conexao」; private static String NOME_BANCO =「Cadastro」; 私人上下文上下文; SQLiteDatabase BANCO_DE_DADOS = null;' – SHENN17 2012-07-05 13:23:43

+0

請參閱編輯:)您正在使用無效的路徑/名稱 – Doomsknight 2012-07-05 13:29:20

+0

我不會忽略無效的路徑/名稱? – SHENN17 2012-07-05 13:30:13