简体   繁体   中英

NullPointer exception and FileInputStream

I have a NPE when I execute the line FileInputStream ifs=openFileInput(fileName); I get a NPE. I get the same NPE when I execute

FileInputStream ifs=getApplicationContext().openFileInput(fileName);

The full stacktrace is here.

07-25 22:00:35.852  25063-25063/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
at com.ollien.flashcarder.MainActivity.setPagerFile(MainActivity.java:353)
at com.ollien.flashcarder.SetsFragment$1.onItemClick(SetsFragment.java:47)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1215)
at android.widget.ListView.performItemClick(ListView.java:4506)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2959)
at android.widget.AbsListView$1.run(AbsListView.java:3645)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:5751)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
at dalvik.system.NativeStart.main(Native Method)

Could someone help? I don't quite know why this is happening. fileName is not null, and is equal to "hi there.txt" Thanks!

解决方案:确保您的Context不为null,我通过函数参数将其传入,例如public void setPagerFile(String fileName,Context mContext){然后使用mContext作为您的上下文。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM