I am working on FileObserver code now. I have the following code and if I run it, I am getting the NullPointerException.
public class FileModificationService extends Service{
private MyFileObserver fileOb;
private static final int MAX_FO = 1;
private List<MyFileObserver> fileOb_list = new ArrayList<MyFileObserver>();
@Override
public void onCreate() {
if (!EnvironmentUtilsStatic.is_external_storage_available()) {
Toast.makeText(FileModificationService.this, "SDCARD is not available!", Toast.LENGTH_SHORT).show();
return;
}
File sdcard = new File("/storage/sdcard/DCIM/Camera/SAMPLE IMAGES/");
if (sdcard == null) {
return;
} else {
fileOb_list.clear();
num_of_fos = 0;
createFileObs(sdcard);
}
}
//only create fileobserver for folders
int num_of_fos = 0;
private void createFileObs(File f) {
if (num_of_fos > MAX_FO) {
return;
}
if (!f.isDirectory()) {
MyFileObserver aFileOb = new MyFileObserver(f.getAbsolutePath());
fileOb_list.add(aFileOb);
} else {
fileOb = new MyFileObserver(f.getAbsolutePath());
MyFileObserver aFileOb = new MyFileObserver(f.getAbsolutePath());
fileOb_list.add(aFileOb);
num_of_fos++;
try {
for (File currentFile : f.listFiles()) {
createFileObs(currentFile);
}
} catch (Exception e) {
Log.e("Error", e.toString());
}
}
}
@Override
public void onStart(Intent intent, int startid) {
fileOb.startWatching();
fileOb = new MyFileObserver("/storage/sdcard/DCIM/Camera/SAMPLE IMAGES/");// added code
for (int i = 0; i < fileOb_list.size(); ++i) { //This is line no. 60
fileOb_list.get(i).startWatching();
}
Toast.makeText(this.getApplicationContext(), "start monitoring file modification", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
fileOb.stopWatching();
for (int i = 0; i < fileOb_list.size(); ++i) {
fileOb_list.get(i).stopWatching();
}
Toast.makeText(this.getApplicationContext(), "stop monitoring file modification", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
And I am getting the following logcat -
09-17 13:05:55.915: E/AndroidRuntime(2596): FATAL EXCEPTION: main
09-17 13:05:55.915: E/AndroidRuntime(2596): java.lang.RuntimeException: Unable to start service roman10.tutorial.fileobserver.FileModificationService@b11293c0 with Intent { cmp=roman10.tutorial.fileobserver/.FileModificationService }: java.lang.NullPointerException
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2721)
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.app.ActivityThread.access$1900(ActivityThread.java:141)
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.os.Looper.loop(Looper.java:137)
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-17 13:05:55.915: E/AndroidRuntime(2596): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 13:05:55.915: E/AndroidRuntime(2596): at java.lang.reflect.Method.invoke(Method.java:525)
09-17 13:05:55.915: E/AndroidRuntime(2596): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-17 13:05:55.915: E/AndroidRuntime(2596): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-17 13:05:55.915: E/AndroidRuntime(2596): at dalvik.system.NativeStart.main(Native Method)
09-17 13:05:55.915: E/AndroidRuntime(2596): Caused by: java.lang.NullPointerException
09-17 13:05:55.915: E/AndroidRuntime(2596): at roman10.tutorial.fileobserver.FileModificationService.onStart(FileModificationService.java:60)
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.app.Service.onStartCommand(Service.java:450)
09-17 13:05:55.915: E/AndroidRuntime(2596): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2704)
09-17 13:05:55.915: E/AndroidRuntime(2596): ... 10 more
This code was actually working previously. I did not make any changes in the code. But, when I try to run this code, after changing my emulator from Google to Intel x86, the problem has started. But, I tried to convert the emulator back to Google. But also, the code did not work.
I could not understand, why this code is not working.
Please guide me, with some suggestions and if possible, with tutorials. Thanks in advance.
You need to initialize fileOb
before using it in fileOb.startWatching()
in onStart()