gpt4 book ai didi

java - FileInputStream 在 .close() NPE 上崩溃

转载 作者:行者123 更新时间:2023-12-02 06:26:20 24 4
gpt4 key购买 nike

为什么我的应用程序崩溃了?它在 fis.close(); 上崩溃线。这是在 doInBackgroundAsyncTask的方法.

    String collected = null;
FileInputStream fis = null;
try {
fis = openFileInput(FILENAME);
byte[] dataArray = new byte[fis.available()];
while (fis.read(dataArray) != -1) {
collected = new String(dataArray);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fis.close();
return collected;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}

-

12-10 14:12:47.431: E/AndroidRuntime(701): FATAL EXCEPTION: AsyncTask #1
12-10 14:12:47.431: E/AndroidRuntime(701): java.lang.RuntimeException: An error occured while executing doInBackground()
12-10 14:12:47.431: E/AndroidRuntime(701): at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-10 14:12:47.431: E/AndroidRuntime(701): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-10 14:12:47.431: E/AndroidRuntime(701): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-10 14:12:47.431: E/AndroidRuntime(701): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-10 14:12:47.431: E/AndroidRuntime(701): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-10 14:12:47.431: E/AndroidRuntime(701): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-10 14:12:47.431: E/AndroidRuntime(701): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-10 14:12:47.431: E/AndroidRuntime(701): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-10 14:12:47.431: E/AndroidRuntime(701): at java.lang.Thread.run(Thread.java:856)
12-10 14:12:47.431: E/AndroidRuntime(701): Caused by: java.lang.NullPointerException
12-10 14:12:47.431: E/AndroidRuntime(701): at com.ollygrov.doughpro.CustomRecipe$loadDatas.doInBackground(CustomRecipe.java:471)
12-10 14:12:47.431: E/AndroidRuntime(701): at com.ollygrov.doughpro.CustomRecipe$loadDatas.doInBackground(CustomRecipe.java:1)
12-10 14:12:47.431: E/AndroidRuntime(701): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-10 14:12:47.431: E/AndroidRuntime(701): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

最佳答案

openFileInput有可能返回null吗?在这种情况下,NullPointerException 将在 byte[] dataArray = new byte[fis.available()]; 行被默默吞掉。

然后执行将跳转到 finally block ,从这里抛出异常,因为它没有在任何 catch block 中处理。

关于java - FileInputStream 在 .close() NPE 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20496826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com