调试的NullPointerException [英] Debug NullPointerException
问题描述
我建立一个音乐播放器。启动应用程序时出现的错误。
I am building a music player. The error occurs when launching the application.
我已经搜索产生的原因:显示java.lang.NullPointerException一词在谷歌,找出这个原因,它似乎像它的投掷某种回空的命令?虽然看logcat的我看到这...
I have searched the term "Caused by: java.lang.NullPointerException" on Google, finding out the cause for this and it seems like it's throwing back null commands of some sort? Whilst looking at the logcat I saw this...
04-06 13:25:16.444: E/AndroidRuntime(30139): Caused by: java.lang.NullPointerException
04-06 13:25:16.444: E/AndroidRuntime(30139): at com.ascendapps.nexplay.SongsManager.getPlayList(SongsManager.java:25)
04-06 13:25:16.444: E/AndroidRuntime(30139): at com.ascendapps.nexplay.MainActivity.onCreate(MainActivity.java:79)
04-06 13:25:16.444: E/AndroidRuntime(30139):
所以,我检查了我的 SongsManager
类,并一直在寻找的 25行
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;
}
当在code看,错误就出在这行如果(home.listFiles(新FileExtensionFilter())长方式&gt; 0){
When looking at the code, the error lies in this line if (home.listFiles(new FileExtensionFilter()).length > 0) {
所以在这做了些研究后,我发现了一种方法,通过改变code绕过这个空异常错误...
So after doing a bit more research on this, I found a way to bypass this null exception error by changing the code to...
public ArrayList<HashMap<String, String>> getPlayList(){
Log.d("testsd",MEDIA_PATH);
File home = new File(MEDIA_PATH);
// if (home.listFiles(new FileExtensionFilter()).length > 0) //don't use this to avoid null pointer exception !
if (home.listFiles(new FileExtensionFilter())!=null) {
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;
}
通过上面一个替换code,它固定零异常错误,但它开始给我另一个错误。这是错误
By replacing the code with the one above, it fixed the null exception error but it started giving me another error. This is the error
04-06 13:36:33.754: E/AndroidRuntime(30586): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-06 13:36:33.754: E/AndroidRuntime(30586): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
04-06 13:36:33.754: E/AndroidRuntime(30586): at java.util.ArrayList.get(ArrayList.java:308)
04-06 13:36:33.754: E/AndroidRuntime(30586): at com.ascendapps.nexplay.MainActivity.playSong(MainActivity.java:281)
04-06 13:36:33.754: E/AndroidRuntime(30586): at com.ascendapps.nexplay.MainActivity.onCreate(MainActivity.java:82)
所以,我检查了我的MainActivity类别,这些都是错误是在 mp.setDataSource(songsList.get(songIndex)获得(songPath))行;
订单281(全功能)
/**
* Function to play a song
* @param songIndex - index of song
* */
public void playSong(int songIndex){
// Play song
try {
mp.reset();
mp.setDataSource(songsList.get(songIndex).get("songPath"));
mp.prepare();
mp.start();
// Displaying Song title
String songTitle = songsList.get(songIndex).get("songTitle");
songTitleLabel.setText(songTitle);
// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.btn_pause);
// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
这是行82
// By default play first song
playSong(0);
我一直在寻找这些code一段时间。我一直在寻找新的错误,但我真的不知道该怎么改?我试着改变0到一个更大的数字,如下面的1,5,10,15,20和什么都没有发生。同样的错误。我看着281线。
I've been looking at these code for a while. I've been looking at the new errors, but I don't really know what to change? I've tried changing the 0 to a larger number such as the following 1,5,10,15,20 and nothing happened. Same errors. I looked at line 281.
如果我读这些logcats错了,请让我知道。此外,如果你觉得我应该恢复我的code回到那里我得到的零点异常错误的点,如果你知道如何从那里修复乳头也让我知道了。并就如何prevent发生下一次这个错误的一些技巧。感谢您的阅读!
If I am reading these logcats wrong, please let me know. Also If you think I should revert my code back to the point where I get the null point exception error, and if you know how to fix tit from there also let me know. And some tips on how to prevent this error occurring next time. Thanks for reading!
推荐答案
如果你的 NullPointerException异常
是由这个引起的。
If your NullPointerException
is being caused by this
home.listFiles(new FileExtensionFilter()).length
然后
home.listFiles(new FileExtensionFilter())
似乎是空
。这可能是因为的 listFiles
函数可以返回空
:
抽象路径名表示此抽象路径名表示的目录中的文件和目录的数组。如果目录是空的数组将为空。返回null如果此抽象路径名不表示一个目录,或者发生I / O错误。
An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
这意味着 MEDIA_PATH
不在目录路径,你认为它是。
Which implies that MEDIA_PATH
is not the directory path that you think it is.
在超出界限异常是由这一行造成的:
The out-of-bounds exception is caused by this line:
mp.setDataSource(songsList.get(songIndex).get("songPath"));
含义 songIndex
在 songsList
不存在。正如你的例外规定:
Meaning songIndex
does not exist in songsList
. As stated in your exception:
产生的原因:java.lang.IndexOutOfBoundsException:无效指数为0,大小为0
您不能引用元素0,这是的的第一个元素的,在零元素列表。
You can't reference element 0, which is the first element, in a zero-element list.
由于 songsList
被添加到只在 MEDIA_PATH
目录中的数据的结果,而 MEDIA_PATH
是某种错误的...希望,当你解决 MEDIA_PATH
,这两个问题都将迎刃而解。
Since songsList
is being added to only as a result of the data found in the MEDIA_PATH
directory, and MEDIA_PATH
is somehow erroneous... Hopefully when you solve the problem with MEDIA_PATH
, both problems will be solved.
这篇关于调试的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!