按照这个运行 前面测试都没问题 运行到最后pl.addptPlayList(p)就提示空指针
这是PlayList类的
//将歌曲添加到普通播放列表
public void addptPlayList(PlayListCollection playListCollection) {
System.out.println("请输入您想添加的普通列表名称");
String str = s.next();
if(playListCollection.getPlayListMap().containsKey(str)) {
System.out.println("请输入歌曲id");
song.setId(s.next());
System.out.println("请输入歌曲名字");
song.setName(s.next());
System.out.println("请输入歌手名字");
song.setSinger(s.next());
}
else {
System.out.println("您的播放列表里没有该列表");
}
}
这是PlayListCollection类的
public class PlayListCollection {
private Map<String,PlayList> playListMap;
public PlayListCollection() {
}
public PlayListCollection(Map<String, PlayList> playListMap) {
super();
this.playListMap = playListMap;
}
public Map<String, PlayList> getPlayListMap() {
return playListMap;
}
public void setPlayListMap(Map<String, PlayList> playListMap) {
this.playListMap = playListMap;
}
Map<String, PlayList> plc = new HashMap<String, PlayList>();
Scanner s = new Scanner(System.in);
//添加播放列表
public void addPlayList(PlayList playList) {
PlayList pl = new PlayList();
System.out.println("请输入您要添加的播放列表名称");
String str = s.next();
pl.setPlayListName(str);
plc.put(str, pl);
System.out.println("添加成功");
}
//删除播放列表
public void deletePlayList(PlayList playList) {
System.out.println("请输入您要删除的播放列表");
plc.remove(s.next());
System.out.println("删除成功");
}
//通过名字查询播放列表
public PlayList searchPlayListByName(PlayList playliset) {
System.out.println("请输入您要查询的播放列表");
String k = s.next();
Set<String> keySet = plc.keySet();
for(String key:keySet) {
if(k.equals(key)) {
break;
}
}
System.out.println("您所查找的播放列表存在!\n该播放列表名称为:"+k);
return plc.get(k);
}
//显示所有播放列表名称
public void displayListName() {
Iterator<String> a = plc.keySet().iterator();
if(a.hasNext()) {
Set<String> keySet = plc.keySet();
for(String key:keySet) {
System.out.println(key);
}
}
else
System.out.println("还没有播放列表");
}
}
这是测试类
public static void main(String[] args) {
// TODO Auto-generated method stub
//PlayList pL = new PlayList("主播放列表",musicList);
PlayListCollection p = new PlayListCollection();
//pL.addToPlayList(song);
PlayList pl = new PlayList();
p.addPlayList(pl);
p.addPlayList(pl);
p.displayListName();
p.searchPlayListByName(pl);
p.displayListName();
p.deletePlayList(pl);
p.displayListName();
pl.addptPlayList(p);
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星