我有一个用户输入艺术家的程序,它使用Last.fm搜索该艺术家,类似艺术家和类似曲目的顶部曲目。我为每个按钮都有三个按钮,但当我点击类似曲目的第三个按钮时,它不会转到该页面并给出错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at Server$9.actionPerformed(Server.java:675)
...
我在下面发布了相关代码。另外两个按钮工作得很好,所以我不知道这个按钮是什么。任何输入都会非常有用!如果您需要我添加更多代码,请告诉我。
btnSimilarTrack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MusicDatabase.getContentPane().removeAll();
MusicDatabase.getContentPane().add(btnSearch);
MusicDatabase.getContentPane().add(btnProfile);
MusicDatabase.getContentPane().add(btnSignOut);
MusicDatabase.getContentPane().add(lblEnterArtistForSimilarTrack);
MusicDatabase.getContentPane().add(lblEnterSongForSimilarTrack);
MusicDatabase.getContentPane().add(ArtistInputForSimilarTrack);
MusicDatabase.getContentPane().add(SongInputForSimilarTrack);
MusicDatabase.getContentPane().add(btnGoSearch);
MusicDatabase.getContentPane().repaint();
btnGoSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if
(ArtistInputForSimilarTrack.getText().isEmpty()||SongInputForSimilarTrack.getText().isEmpty()) {
return;
}
try {
//user types in artist, goes to site for SIMILAR TRACKS
Scanner in = new Scanner (System.in);
String site = "http://ws.audioscrobbler.com/2.0/?method=track.getsimilar&artist=BLANKARTIST&track=BLANKTRACK&api_key=bc5957059ef480bb46bd37e834375495";
System.out.println("Enter an artist name: ");
String artist = ArtistInputForSimilarTrack.getText();
System.out.println("Enter the song title: ");
String song = SongInputForSimilarTrack.getText();
String newSite = site.replace("BLANKARTIST", artist); //replace "BLANKARTIST" with artist in URL
String finalSite = newSite.replace("BLANKTRACK", song); //replace "BLANKTRACK with track in URL
//System.out.println("Old site: " + site);
//System.out.println("New site: " + newSite);
System.out.println("Site " + finalSite);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document similar = db.parse(new URL(finalSite).openStream());
similar.getDocumentElement().normalize();
//System.out.println(similar.getDocumentElement().getNodeName());
NodeList nList = similar.getElementsByTagName("track");
System.out.println("Most similar track to " + song + " by " + artist + ":");
for (int t = 0; t < 1; t++) {
Node nNode = nList.item(t);
//System.out.println("\n" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("\nSong name: " + eElement.getElementsByTagName("name").item(0).getTextContent());
//pressing enter makes label display similar artists
lblDisplaySimilarTrack.setText("<html><center>" + "Most similar track to " + song + " by " + artist + ": " + "<br>" + "Song name: " + eElement.getElementsByTagName("name").item(0).getTextContent() + "</center></html>");
MusicDatabase.getContentPane().add(lblDisplaySimilarTrack);
MusicDatabase.getContentPane().add(btnSignOut);
MusicDatabase.getContentPane().repaint();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}