5

Sorry for my formatting I am newbie to android as well as to stackoverflow cant submit all of my logcat due to some formatting error

in short I am getting following error:

at com.youmasti.mp3test.MainActivity.findsongs(MainActivity.java:45)
at com.youmasti.mp3test.MainActivity.onCreate(MainActivity.java:27)

Here is my code:

Main Activity

public class MainActivity extends AppCompatActivity {

ListView lv;
String items;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv= (ListView)findViewById(R.id.lvPlaylist);
    //File fTest = Environment.getExternalStorageDirectory();
    ArrayList<File> mySongs = findsongs(Environment.getExternalStorageDirectory());

    for (int i = 0; i < mySongs.size(); i++) {
        toast(mySongs.get(i).getName().toString());
    }
}

public ArrayList<File> findsongs(File root) {
    ArrayList<File> al= new ArrayList<File>();
    File[] file= root.listFiles();

    for (File singlefile : file) {
        if (singlefile.isDirectory() && !singlefile.isHidden()) {
            al.addAll(findsongs(singlefile));
        } else {
            if (singlefile.getName().endsWith(".mp3")) {
                al.add(singlefile);
            }
        }
    }
    return al;
}

public void toast (String text) {
    Toast.makeText(getApplicationContext(),text,Toast.LENGTH_SHORT).show();
}

List item:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.youmasti.mp3test">    
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

LogCat

   08-30 10:39:30.227    2441-2441/? I/art﹕ Not late-enabling -Xcheck:jni        (already on)
   08-30 10:39:30.227    2441-2441/? I/art﹕ Late-enabling JIT
   08-30 10:39:30.229    2441-2441/? I/art﹕ JIT created with                code_cache_capacity=2MB compile_threshold=1000
   08-30 10:39:30.346    2441-2441/? W/System﹕ ClassLoader referenced        unknown path: /data/app/com.youmasti.mp3test-1/lib/x86
   08-30 10:39:30.527    2441-2441/com.youmasti.mp3test D/AndroidRuntime﹕        Shutting down VM
       --------- beginning of crash
   08-30 10:39:30.528    2441-2441/com.youmasti.mp3test E/AndroidRuntime﹕        FATAL EXCEPTION: main
       Process: com.youmasti.mp3test, PID: 2441
       java.lang.RuntimeException: Unable to start activity ComponentInfo       {com.youmasti.mp3test/com.youmasti.mp3test.MainActivity}:        java.lang.NullPointerException: Attempt to get length of null array
               at android.app.ActivityThread.performLaunchActivity       (ActivityThread.java:2416)
               at android.app.ActivityThread.handleLaunchActivity                     (ActivityThread.java:2476)
                      at android.app.ActivityThread.-wrap11                                                        (ActivityThread.java)
               at android.app.ActivityThread$H.handleMessage       (ActivityThread.java:1344)
               at android.os.Handler.dispatchMessage(Handler.java:102)
               at android.os.Looper.loop(Looper.java:148)
               at android.app.ActivityThread.main(ActivityThread.java:5417)
               at java.lang.reflect.Method.invoke(Native Method)
               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run       (ZygoteInit.java:726)
               at com.android.internal.os.ZygoteInit.main       (ZygoteInit.java:616)
        Caused by: java.lang.NullPointerException: Attempt to get length of        null array
               at com.youmasti.mp3test.MainActivity.findsongs       (MainActivity.java:45)
               at com.youmasti.mp3test.MainActivity.onCreate       (MainActivity.java:27)
               at android.app.Activity.performCreate(Activity.java:6237)
               at android.app.Instrumentation.callActivityOnCreate       (Instrumentation.java:1107)
               at android.app.ActivityThread.performLaunchActivity       (ActivityThread.java:2369)
               at android.app.ActivityThread.handleLaunchActivity       (ActivityThread.java:2476)
               at android.app.ActivityThread.-wrap11(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1344)
   at android.os.Handler.dispatchMessage(Handler.java:102)
3
  • "java.lang.RuntimeException: Unable to start activity ComponentInfo", you don't have that activity declared in manifest Commented Aug 30, 2015 at 15:59
  • Have you checked that your ExternalStorageDirectory has files? That's why you are getting NullPointerException. Commented Aug 30, 2015 at 16:00
  • Looks like File[] file might be null. Commented Aug 30, 2015 at 16:01

3 Answers 3

11

You have to point to us where is the line 45.

But I would guess it is this line

for (File singlefile : file) {

if yes, the problem is because listFiles() return null

It can return null for several reasons, root not exists, or root is not a folder, or most likely, as you are working with Android, you missed to add the permission in your manifest

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

EDIT:

after your edit, I believe your manifest has some problem

please check a sample here

https://developer.android.com/samples/BasicContactables/AndroidManifest.html

EDIT2:

https://developer.android.com/preview/features/runtime-permissions.html

for android 6.0, you need to request permission

if (checkSelfPermission(Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (shouldShowRequestPermissionRationale(
            Manifest.permission.READ_CONTACTS)) {
        // Explain to the user why we need to read the contacts
    }

    requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
    // app-defined int constant

    return;
}
Sign up to request clarification or add additional context in comments.

8 Comments

thanks for your reply.Y es 45th line is for (File singlefile : file) { I have already added that permission in Manifest folder, Please see my manifest in above post.
Are you testing on android 6.0 emulator? or android 6.0 device?
when I tested it on 6.0 emulator I gave me above error but I just tried it on my cell phone and it is working fine. how come ?
in android 6.0, you need to requestPermission for "dangerous" permission, edited my answer to include the link about request permission
Maybe you could accept the answer, and start a new question if you meet any further problems when implementing request permission
|
1

Assume that MainActivity.java:45 is this line:

for (File singlefile : file) {

It means that file is null.

According to the javadoc of File.listFiles():

Returns null if this abstract pathname does not denote a directory

So value of root is not a path name.

1 Comment

thanks for your answer, I am just trying to scan phone storage to find some file and Environment.getExternalStorageDirectory()); is that location
0

your

  listFiles()

method may return null and root is not any path (use as)

 Environment.getExternalStorageDirectory() + path like directory name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.