Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Caused by: java.lang.NullPointerException: Attempt to get length of null array
                                                      at com.example.mahakal.musicplayer.SongsManager.getPlayList(SongsManager.java:25)


What I have tried:

Java
<pre>public ArrayList<HashMap<String, String>> getPlayList(){
        File home = new File(MEDIA_PATH);

        if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                HashMap<String, String> song = new HashMap<String, String>();
                song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
                song.put("songPath", file.getPath());

                // Adding each song to SongList
                songsList.add(song);
            }
        }
        // return songs list array
        return songsList;
    }
Posted
Updated 16-Feb-17 10:05am

Would be good to know at which line the execption occurred.

If it is at the first if condition, listFiles() has returned null. See File (Java Platform SE 7 )[^]:
Quote:
Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

So check if MEDIA_PATH is an existing and readable directory.
 
Share this answer
 
Quote:
Caused by: java.lang.NullPointerException: Attempt to get length of null array

Something failed in your code, and only you can do something because we can't guess where is the problem from our remote place.
Use the debugger to see your code execute and see where it fail to put data in your variable.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900