I am working with file in android. I used below code and it worked well in Note 2 that used OS 4.4.2. However, It appeared the error as below report when I run in OS 5.0.1 (Galaxy S4). What is happen in my code? Could you suggest for me to solve it
private boolean checkAudioFile()
{
ArrayList<String> datacheck=null;
try{
String filepath = Environment.getExternalStorageDirectory().getPath();
String filename=filepath+"/" + FILE_FOLDER + "/" + "file_list.txt" ;
BufferedReader in = new BufferedReader(new FileReader(filename));
String audio_name;
datacheck = new ArrayList<String>();
while ((audio_name = in.readLine()) != null) {
datacheck.add(audio_name);
}
in.close();
} catch (IOException e) {
System.out.println("File Read Error");
}
if(datacheck.isEmpty())
return true;
else
return false;
}
This is my error in OS 5.0.1
11-16 20:42:48.483: I/System.out(16967): File Read Error
11-16 20:42:48.483: D/AndroidRuntime(16967): Shutting down VM
11-16 20:42:48.483: E/AndroidRuntime(16967): FATAL EXCEPTION: main
11-16 20:42:48.483: E/AndroidRuntime(16967): Process: com.example.readfileapp, PID: 16967
11-16 20:42:48.483: E/AndroidRuntime(16967): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.readfileapp/com.example.readfileapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.isEmpty()' on a null object reference
11-16 20:42:48.483: E/AndroidRuntime(16967): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
11-16 20:42:48.483: E/AndroidRuntime(16967): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
11-16 20:42:48.483: E/AndroidRuntime(16967): at android.app.ActivityThread.access$900(ActivityThread.java:177)
11-16 20:42:48.483: E/AndroidRuntime(16967): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
11-16 20:42:48.483: E/AndroidRuntime(16967): at android.os.Handler.dispatchMessage(Handler.java:102)
11-16 20:42:48.483: E/AndroidRuntime(16967): at android.os.Looper.loop(Looper.java:145)
11-16 20:42:48.483: E/AndroidRuntime(16967): at android.app.ActivityThread.main(ActivityThread.java:5942)
11-16 20:42:48.483: E/AndroidRuntime(16967): at java.lang.reflect.Method.invoke(Native Method)
11-16 20:42:48.483: E/AndroidRuntime(16967): at java.lang.reflect.Method.invoke(Method.java:372)
11-16 20:42:48.483: E/AndroidRuntime(16967): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
11-16 20:42:48.483: E/AndroidRuntime(16967): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
11-16 20:42:48.483: E/AndroidRuntime(16967): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.isEmpty()' on a null object reference
11-16 20:42:48.483: E/AndroidRuntime(16967): at com.example.readfileapp.MainActivity.checkAudioFile(MainActivity.java:622)
11-16 20:42:48.483: E/AndroidRuntime(16967): at com.example.readfileapp.MainActivity.onCreate(MainActivity.java:172)
11-16 20:42:48.483: E/AndroidRuntime(16967): at android.app.Activity.performCreate(Activity.java:6289)
11-16 20:42:48.483: E/AndroidRuntime(16967): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
11-16 20:42:48.483: E/AndroidRuntime(16967): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
11-16 20:42:48.483: E/AndroidRuntime(16967): ... 10 more
You need to perform a null check.
You ArrayList can be empty but also it can be null and then you cant call isEmpty()
Change it to this:
if(datacheck!=null && datacheck.isEmpty())
Hope this helps.