当前位置: 首页 > 面试题库 >

在Java程序中获取运行时错误

时同
2023-03-14
问题内容

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



 类似资料:
  • 我正在使用一个在线编译器来运行这个简单的代码,但是它给出了一个运行时错误,“Could not find or load class main”没有其他语法错误。请提出解决方案。

  • 问题内容: 我需要解决JDK 1.5中的Java错误,该错误已在1.6中修复。我正在使用以下条件: 这对其他JVM有用吗?有更好的方法来检查吗? 问题答案: 是每个JVM中都存在的系统属性。有两种可能的格式: Java的8或降低:1.6.0_23,1.7.0,1.7.0_80,1.8.0_211 Java的9或更高:9.0.1,11.0.4,12,12.0.1 这是提取主要版本的技巧:如果它是版本

  • 升级到最新的android studio(从3.3.1升级到3.3.2)后,我在运行应用程序时出现以下错误: 我尝试过的事情: 1) 无效缓存/重新启动2)已清理项目3)重建项目(成功) 但是我的问题仍然没有解决。我不知道是什么类型的错误。如果没有,请告诉,如何将我的Android Studio降级到以前的版本。 我的项目级成绩文件:

  • 问题内容: 我想在几种不同条件下在linux shell中执行某些操作,并能够输出每次执行的执行时间。 我知道我可以编写一个可以执行此操作的perl或python脚本,但是有什么方法可以在shell中执行它吗?(碰巧是bash) 问题答案: 使用内置关键字: 例:

  • 当我使用ide运行我的springboot应用程序时,它工作正常。 但当我运行它的jar时,我得到了白标签错误页面。 白标签错误页面 此应用程序没有 /error的显式映射,因此您将此视为后备。 2018年10月9日星期二15:42:47出现意外错误(类型=未找到,状态=404)/WEB-INF/views/index。jsp

  • 我是编程和学习Java的新手。 我正在尝试打包一个Java Spring Boot应用程序。当我使用命令“mvnw package”时,我得到以下错误: /watchlist/WatchlistApplicationTests由最新版本的Java运行时(类文件版本55.0)编译,该版本的Java运行时仅识别高达52.0的类文件版本 通过在线阅读,我了解到我使用旧Java版本存在问题。 文件版本55