我必须为Java类简介创建一个项目,并在最终解决语法错误之后,单击播放或保存按钮时,该程序出现运行时错误。
//C:\\Users\\Andrew\\Downloads\\never gonna give you up.wav //Andrew Douglas //Imports import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.io.*; import javax.sound.sampled.*; import javax.swing.filechooser.*; import javax.swing.JTable; //Creates class public class JPlayer extends JFrame implements ActionListener { //Sets up form items and necessary globals JButton save, play, stop, loop; JFileChooser dialog; JTable table; String Artist, Song, Album, Loc; Object[][] data; int n = 1; //Makes the library, with a 51 song limit. JLibrary[] addedSong = new JLibrary[50]; public JPlayer() { super ("JPlayer"); //Creates frame this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("jPlayer"); this.setSize(800, 600); //Makes titles for table String[] columnNames = {"Artist", "Song", "Album", "Location"}; //Gives one value for array addedSong[0] = new JLibrary ("Rick Astley", "NGGYU", "UnKnown", "C:\\Users\\Andrew\\Downloads\\never gonna give you up.wav"); //Adds it to table array Object[][] data = { { (addedSong[0].returnArtist()), (addedSong[0].returnSong()), (addedSong[0].returnAlbum()), (addedSong[0].returnFile()) } }; //Creates table JTable table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); //Lets it sort the rows table.setAutoCreateRowSorter(true); //Creates the scroller JScrollPane scrollPane = new JScrollPane(table); //Makes the save file dialog and the play and save buttons dialog = new JFileChooser(); play = new JButton ("Play Song"); save = new JButton ("Save a file"); //Adds the button listeners save.addActionListener(this); play.addActionListener(this); //Adds buttons to panel JPanel buttons = new JPanel(); buttons.add(save); buttons.add(play); //Puts the buttons at the bottom add(buttons, BorderLayout.SOUTH); add(scrollPane); this.setVisible(true); } //Creates action listener for button public void actionPerformed(ActionEvent e) { if (e.getSource() == save) { dialog.setFileFilter(new FileNameExtensionFilter("Wave File (*.wav)")); int returnVal = dialog.showSaveDialog(JPlayer.this); if (returnVal == dialog.APPROVE_OPTION) { File file = dialog.getSelectedFile(); addToLibrary("", "", "", file.getName()); } } else if (e.getSource() == play) { String holder2; Object holder; holder = table.getValueAt(table.getSelectedRow(), 3); try { File soundFile = new File(holder.toString()); AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile); Clip clip = AudioSystem.getClip(); clip.open(audioIn); clip.start(); } catch (UnsupportedAudioFileException f) { f.printStackTrace(); } catch (IOException f) { f.printStackTrace(); } catch (LineUnavailableException f) { f.printStackTrace(); } } } public static void main(String[]args) { new JPlayer(); } public void addToLibrary(String art, String song, String alb, String file) { addedSong[n] = new JLibrary(art, song, alb, file); int j = 0; while (n >= 0) { Object[][] data = { { addedSong[(n-j)], } }; j = j+1; } n = n +1; } }
每当我单击播放按钮开始播放文件中的音乐时,都会出现此错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at JPlayer.actionPerformed(JPlayer.java:86) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6505) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:688) at java.awt.EventQueue$3.run(EventQueue.java:686) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:702) at java.awt.EventQueue$4.run(EventQueue.java:700) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:699) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
当我单击保存按钮时,出现此错误:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Extensions must be non-null and not empty at javax.swing.filechooser.FileNameExtensionFilter.<init>(FileNameExtensionFilter.java:76) at JPlayer.actionPerformed(JPlayer.java:75) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6505) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:688) at java.awt.EventQueue$3.run(EventQueue.java:686) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:702) at java.awt.EventQueue$4.run(EventQueue.java:700) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:699) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
谁能告诉我什么地方出了问题或如何解决?任何帮助,将不胜感激,我在这个东西上有点慢:)
对于播放按钮错误:通过计数行(75-> 86),我推断出该语句中的错误
holder = table.getValueAt(table.getSelectedRow(), 3);
这是因为表(字段)为空。在构造函数中,您将填充一个名为table的局部变量,该变量将隐藏相同名称的字段。更换
JTable table = new JTable(data, columnNames);
通过
table = new JTable(data, columnNames);
对于保存按钮错误,看来应该为FileNameExtensionFilter的构造函数提供第二个参数“ wav”