【问题标题】:Getting error on opening sqlite db in assets folder在资产文件夹中打开 sqlite db 时出错
【发布时间】:2017-09-17 23:15:44
【问题描述】:

我在尝试访问Assets 文件夹中的db 时收到NullpointerExceptiondbAssets 文件夹上以红色表示。

Image containing Db in Assets

public void createdatabase() throws IOException
{
    System.out.println(" came into Create database ");
    //Open your local db as the input stream
    InputStream myinput = mycontext.getAssets().open(DB_NAME);
    System.out.println(" myinput = "+myinput.toString());
    System.out.println(" Got input ");

    // Path to the just created empty db
    String outfilename = DB_PATH + DB_NAME;
    System.out.println(" outfilename = "+outfilename);

    //Open the empty db as the output stream
    OutputStream myoutput = new FileOutputStream(outfilename);

    // transfer byte to inputfile to outputfile
    System.out.println(" created outputStream");
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myinput.read(buffer))>0) {
        myoutput.write(buffer,0,length);
    }

    //Close the streams
    myoutput.flush();
    myoutput.close();
    myinput.close();
}

有什么方法可以检查String中的db值吗?

【问题讨论】:

  • 发布您从资产中获取数据库的代码
  • 但代码在 myinput 处停止执行,显示 nullPointerException
  • 你得到的不是获取数据库的正确方法
  • 私有静态 String getDatabasePath(Context _context, String _databaseName) { return "/data/data/"+_context.getPackageName()+"/databases/"+_databaseName; }

标签: android sqlite nullpointerexception


【解决方案1】:

DB_NAME 与 assets 文件夹中的文件名不匹配。

有什么理由不告诉你这些值吗?

【讨论】:

  • 你是怎么得出这个结论的?那会抛出FileNotFoundException,而不是NullPointerException。我的意思是,我不是说你错了。他们很可能有错误的名称,但显然,直接的问题是 NPE。我错过了一些 cmets 吗?
  • 好吧告诉DB_NAME的值。和文件名。比较。他们不相等。如果我建议它们不相等,您为什么不发布这两个值?我什至让你说出价值观。难以置信。
  • 我不是 OP。我只是问他们是否在我错过的一些评论中提到了DB_NAME 的价值,这些评论已被删除。
  • 对不起,我监督了。这是一个如此令人困惑的帖子。确实是空指针异常。对不起。我认为是一个空上下文。
  • 是的,我也打赌。
猜你喜欢
  • 2010-12-28
  • 2014-11-18
  • 2014-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 2018-12-31
  • 2018-12-17
相关资源
最近更新 更多