arun s b
arun s b

Reputation: 1

unfortunately the application has stopped

I am trying to list the files in the sdcard and sort them according to last modified date.

I dont get any errors when I build it but when I start it in emulator logcat shows an error and the emulator show "unfortunately the application has stopped". Please help me understand the error message and how to solve this.

Here is the code I use:

main activity.java public class MainActivity extends Activity {

@SuppressWarnings("rawtypes")
public final class ComparatorImplementation implements Comparator
{
    public int compare(Object f1, Object f2)
    {
      if (((File) f1).lastModified() < ((File) f2).lastModified())
      {
        return -1;
      }
      else if (((File) f1).lastModified() > ((File) f2).lastModified())
      {
        return 1;
      }
      else
      {
        return 0;
      }
    }
}

@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    TextView info = (TextView)findViewById(R.id.info);


    File dir = new File("/storage/sdcard/");

    if(dir.isDirectory())
    {
      // Fetching the list from the directory
      File[] files = dir.listFiles();

      // Sort files by date.
     Arrays.sort(files, new ComparatorImplementation());

      //Prints files in order by last modified date
      for(File file:files)
      {
          info.setText( "list of files according to last modified date \n " + file.getName());
      }
    }       
    }
}

This is the logcat output

10-14 01:58:10.470: E/AndroidRuntime(3399): FATAL EXCEPTION: main
10-14 01:58:10.470: E/AndroidRuntime(3399): Process: com.listfiles, PID: 3399
10-14 01:58:10.470: E/AndroidRuntime(3399): java.lang.RuntimeException: Unable to start activity          ComponentInfo{com.listfiles/com.listfiles.MainActivity}: java.lang.NullPointerException
10-14 01:58:10.470: E/AndroidRuntime(3399):     at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at android.os.Looper.loop(Looper.java:136)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at java.lang.reflect.Method.invokeNative(Native Method)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at  java.lang.reflect.Method.invoke(Method.java:515)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at dalvik.system.NativeStart.main(Native Method)
10-14 01:58:10.470: E/AndroidRuntime(3399): Caused by: java.lang.NullPointerException
10-14 01:58:10.470: E/AndroidRuntime(3399):     at java.util.TimSort.sort(TimSort.java:169)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at java.util.Arrays.sort(Arrays.java:2023)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at com.listfiles.MainActivity.onCreate(MainActivity.java:55)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at android.app.Activity.performCreate(Activity.java:5231)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-14 01:58:10.470: E/AndroidRuntime(3399):     at                       android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-14 01:58:10.470: E/AndroidRuntime(3399):     ... 11 more

Upvotes: 0

Views: 175

Answers (5)

sjain
sjain

Reputation: 23344

You are using listFiles() which can be null if this file is not a directory.

I can see here:

File dir = new File("/storage/sdcard/");

And then,

dir.listFiles();

public File[] listFiles()
Added in API level 1

Returns an array of files contained in the directory represented by this file. The result is null if this file is not a directory. The paths of the files in the array are absolute if the path of this file is absolute, they are relative otherwise.

Returns an array of files or null.

Upvotes: 0

Angad Tiwari
Angad Tiwari

Reputation: 1768

arun you are not handling the file

File[] files = dir.listFiles();
Arrays.sort(files, new ComparatorImplementation());

case:- suppose if dir path does exist, case:- suppose if dir doesn;t contain the files then,files object is null,and it obviously throws null pointer exception, and what exactly happening here

solution: handle files null pointer condition by:

if(files==null){
Toast.makeText(getApplicationContext(),"directory doesnt contain files",0).show();}
else{
for(File file:files)
  {
      info.setText( "list of files according to last modified date \n " + file.getName());
  }}

Upvotes: 0

MOHRE
MOHRE

Reputation: 1156

that's possible there were no file there. you should check if there is any one with something like:

if (Dir.exists()) {
    // type your code here!
}

Upvotes: 0

Chintan Rathod
Chintan Rathod

Reputation: 26034

You are getting NullPointerException. This means object is not initialized properly.

You can see that there is one line at java.util.Arrays.sort(Arrays.java:2023), so I guess problem is in Arrays.sort() function.

I think there is some problem with following code

File[] files = dir.listFiles();

// Sort files by date.
Arrays.sort(files, new ComparatorImplementation());

listFiles() can return null if the directory is invalid.

Try following code to prevent exception:

File[] files = dir.listFiles();

// Sort files by date.
if(files!=null){
    Arrays.sort(files, new ComparatorImplementation());

    //your rest of the code based on "files" object. If object is `null`, you can
    //not proceed
}

You should also use the method Environment.getExternalStorageDirectory() instead of specifying it manually, as some devices has different paths to the sd-card.

Upvotes: 1

Beena
Beena

Reputation: 2354

In your code,

File[] files = dir.listFiles();

files variable is set to null so it is crashing. Check before sorting if files is null or not.

Upvotes: 0

Related Questions