[英]Reasons for NullPointerException from ContextWrapper.getFilesDir()
I have my app published in Google Play and I have received a Crash report from one user. 我已经在Google Play中发布了我的应用,并且收到了一位用户的当机报告。 Unfortunately, due to anonymity reasons, I do not have the contact details of the reporter and therefore I cannot get any more details on the crash.
不幸的是,由于匿名原因,我没有报告人的详细联系方式,因此我无法获得崩溃的更多详细信息。 I only have available the exception details from the report.
我只有报告中的异常详细信息。 Below is the function where I the exception is thrown.
下面是我抛出异常的函数。 The exception is thrown at the following line:
在以下行引发异常:
File dbDir = new File(this.getFilesDir().getPath()+"/database");
I am trying to find out why a NullPointerException can be thrown at the above line. 我试图找出为什么可以在上一行抛出NullPointerException。
Any suggestions are welcome. 欢迎任何建议。
The full function is below: 完整功能如下:
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.