I have a weird problem where, the first time you start the app, it works well, the 2nd time it crashes, the 3rd works, the 4th crashes and so on..
Here is the logcat:
E/AndroidRuntime(18039): FATAL EXCEPTION: main
E/AndroidRuntime(18039): java.lang.RuntimeException: Unable to start activity Co
mponentInfo{omar.quran1/omar.quran1.Quran}: java.lang.NullPointerException
E/AndroidRuntime(18039): at android.app.ActivityThread.performLaunchActiv
ity(ActivityThread.java:1768)
E/AndroidRuntime(18039): at android.app.ActivityThread.handleLaunchActivi
ty(ActivityThread.java:1784)
E/AndroidRuntime(18039): at android.app.ActivityThread.access$1500(Activi
tyThread.java:123)
E/AndroidRuntime(18039): at android.app.ActivityThread$H.handleMessage(Ac
tivityThread.java:939)
E/AndroidRuntime(18039): at android.os.Handler.dispatchMessage(Handler.ja
va:99)
E/AndroidRuntime(18039): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(18039): at android.app.ActivityThread.main(ActivityThrea
d.java:3835)
E/AndroidRuntime(18039): at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime(18039): at java.lang.reflect.Method.invoke(Method.java:5
07)
E/AndroidRuntime(18039): at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:847)
E/AndroidRuntime(18039): at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:605)
E/AndroidRuntime(18039): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(18039): Caused by: java.lang.NullPointerException
E/AndroidRuntime(18039): at omar.quran1.Quran.getReaders(Quran.java:565)
E/AndroidRuntime(18039): at omar.quran1.Quran.onCreate(Quran.java:414)
E/AndroidRuntime(18039): at android.app.Instrumentation.callActivityOnCre
ate(Instrumentation.java:1047)
E/AndroidRuntime(18039): at android.app.ActivityThread.performLaunchActiv
ity(ActivityThread.java:1722)
E/AndroidRuntime(18039): ... 11 more
So when the code reaches getReaders(), it crashes, and exactly on this line
int lengthtmp=f.listFiles().length;
Here is the function:
public void getReaders()
{
File f = new File(SuraDatabase.Audio_PATH);
if(f!=null)
{
int lengthtmp=f.listFiles().length; //It crashes here - NullPointerException
if(lengthtmp==0||lengthtmp>10)
{
SoundFiles=false;
}
else
{
SoundFiles=true;
File[] readArrays=f.listFiles();
for(int i =0;i<lengthtmp;i++)
{
if(readArrays[i].isDirectory())
{
ReaderOptions.add(readArrays[i].getName());
}
}
}
}
}
Can anyone tell me why is that line returning Null?
Thanks.
First off, Since you are checking if f is null, instead, check if
f.exists(). Because if that doesn’t exist, then there is no way that it can list anything, thus it would be null, which would be causing your exception.This isn’t exactly an answer to your question, but try that and see what happens, and post your results so we can give you a better answer.
I’m also guessing you are somehow deleting this directory after your first run, but it’s just a guess without seeing actual code.