我如何...在音乐播放器中解决此错误 [英] How do i...solve this error in music player

查看:88
本文介绍了我如何...在音乐播放器中解决此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:

<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;
    }

推荐答案

很高兴知道执行发生在哪一行。



如果它在第一个 if 条件, listFiles()返回null。请参阅文件(Java Platform SE 7) [ ^ ]:

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:

如果此抽象路径名执行,则返回null不表示目录,或者发生I / O错误。

Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.



因此,检查 MEDIA_PATH 是否是现有且可读的目录。


So check if MEDIA_PATH is an existing and readable directory.


Quote:

引起:java.lang.NullPointerException:尝试获取null数组的长度

Caused by: java.lang.NullPointerException: Attempt to get length of null array



你的代码中有些东西失败了,只有你可以做某事,因为我们无法猜到我们偏远地方的问题在哪里。

使用调试器看看你的代码是否执行,看看它在哪里无法将数据放入变量中。



当你不这样做时了解您的代码正在做什么或为什么它做它做的事情,答案是调试器

使用调试器来查看您的代码正在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java -application.html [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


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.


这篇关于我如何...在音乐播放器中解决此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆