-1

我必须为我的 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)

任何人都可以通过告诉我出了什么问题或如何解决它来帮助我吗?任何帮助将不胜感激,我对这些东西有点慢:)

4

1 回答 1

0

对于播放按钮错误:通过计算行数 (75 -> 86) 我推断错误在此语句中

holder = table.getValueAt(table.getSelectedRow(), 3);

这是因为表(字段)为空。在构造函数中,您正在填充一个名为 table 的局部变量,该变量隐藏了同名字段。代替

JTable table = new JTable(data, columnNames);

经过

table = new JTable(data, columnNames);

对于保存按钮错误,似乎应该将“wav”作为第二个参数提供给FileNameExtensionFilter的构造函数

于 2013-06-19T14:57:47.237 回答