简体   繁体   English

ContextWrapper.getFilesDir()中出现NullPointerException的原因

[英]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();
        }
    }
}

getFilesDir sometimes returns null. getFilesDir有时会返回null。 It's bug . 是虫子 So you mush check if getFilesDir returned null or not. 因此,您必须检查getFilesDir返回null。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NullPointerException用于Android ContextWrapper - NullPointerException for android ContextWrapper 将数据写入内部文件 - ContextWrapper NullPointerException - Writing Data to Internal File - ContextWrapper NullPointerException BackupManager.dataChanged中ContextWrapper.getPackageName上的NullPointerException - NullPointerException on ContextWrapper.getPackageName in BackupManager.dataChanged 使用SQLiteOpenHelper时ContextWrapper.openOrCreateDatabase中的NullPointerException - NullPointerException in ContextWrapper.openOrCreateDatabase when using SQLiteOpenHelper 在JNI调用中调用getFilesDir()时发生NullPointerException - NullPointerException when Calling getFilesDir() in a JNI Call android.content.contextwrapper.getsystemservice上的java.lang.nullpointerexception(Contextwrapper.java:386) - java.lang.nullpointerexception at android.content.contextwrapper.getsystemservice(Contextwrapper.java:386) 如何解决无法从ContextWrapper错误中降低继承方法的可见性? - How to fix Cannot reduce the visibility of the inherited method from ContextWrapper error? 关闭活动或从扩展ContextWrapper的类中调用finish() - Close activity or call finish() from class that extends ContextWrapper DownloadManager getFilesDir() - DownloadManager getFilesDir() 无法从静态方法Android访问任何静态方法:ContextWrapper类型的getResources() - Cannot access none static methods from static method, Android: getResources() from the type ContextWrapper
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM