gpt4 book ai didi

Android - openOrCreateDatabase - 空指针异常

转载 作者:太空宇宙 更新时间:2023-11-03 10:52:23 27 4
gpt4 key购买 nike

我有我的 DAO 类,当我调用我的类 conexao 时,该方法返回 null,但为什么?

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

最佳答案

很可能 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";

关于Android - openOrCreateDatabase - 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11344914/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com