我的应用已在Google Play中发布,但我收到了一位用户的崩溃报告。不幸的是,由于匿名原因,我没有记者的联系方式,因此我无法获得有关崩溃的更多细节。我只提供报告中的异常详细信息。下面是抛出异常的函数。在以下行抛出异常:
File dbDir = new File(this.getFilesDir().getPath()+"/database");
我试图找出为什么可以在上面的行抛出NullPointerException。
欢迎任何建议。
完整功能如下:
private void ensureDBAvailable() throws IOException{
// The NullPointerException was thrown in the line below
File dbDir = new File(this.getFilesDir().getPath()+"/database");
if (!(dbDir.mkdirs() || dbDir.isDirectory()))
{
//This should never happen, in this case the caller should stop the service
throw(new IOException("Cannot create database directory"));
}
File dbFile = new File(dbDir, MainService.DB_FILENAME);
if(!dbFile.exists())
{
InputStream dbFromApk = null;
OutputStream dbout = null;
try
{
dbFromApk = this.getAssets().open(DB_FILENAME, AssetManager.ACCESS_STREAMING);
//the database file does not exist, let's get it from the APK.
dbout = new FileOutputStream(dbFile);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while((bytesRead = dbFromApk.read(buffer))> 0)
{
dbout.write(buffer, 0, bytesRead);
}
}
finally
{
dbFromApk.close();
dbout.close();
}
}
}