【发布时间】:2017-09-17 23:15:44
【问题描述】:
我在尝试访问Assets 文件夹中的db 时收到NullpointerException。 db 在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