在Java程序中获取运行时错误 [英] Getting runtime errors in Java program

查看:159
本文介绍了在Java程序中获取运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为我的java类简介制作一个项目,在最终整理出语法错误之后,当我点击播放或保存按钮时,我在这个程序中遇到运行时错误。

I have to make a project for my intro to java class, and after finally sorting out the syntax errors, I'm getting runtime errors in this program when I click the play, or save buttons.

//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;

    }
}

每当我点击播放按钮开始播放文件中的音乐,我收到此错误:

Whenever I click the play button to start playing the music in the file, I get this error:

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)

当我点击保存按钮时出现此错误:

And when I click the save button I get this error:

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)

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

Could anyone maybe help me by telling me what's wrong or how to fix it? Any help would be appreciated, I'm a bit slow at this stuff :)

推荐答案

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

For the play button error: by counting lines (75 -> 86) I deduce that the error is in this statement


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

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

This is because table (the field) is null. In the constructor you are populating a local variable called table which is hiding the field of the same name. Replace


JTable table = new JTable(data,columnNames);

by


table = new JTable(data,columnNames);

对于保存按钮错误,它出现应该将wav作为 FileNameExtensionFilter

For the save button error it appears that "wav" should be provided as a second argument to the constructor for FileNameExtensionFilter

这篇关于在Java程序中获取运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆