【问题标题】:JApplet null pointer exception on start开始时 JApplet 空指针异常
【发布时间】:2013-03-24 09:26:39
【问题描述】:

我正在为一个类编写一个小程序,并且认为代码看起来正确,但在小程序启动时我得到一个空指针异常。任何帮助将不胜感激。

错误信息:

java.lang.NullPointerException
在 sun.applet.AppletAudioClip.(AppletAudioClip.java:65)
在 java.applet.Applet.newAudioClip(Applet.java:311)
在 Lab5b.(Lab5b.java:14)
在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct orAccessorImpl.java:57)
在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC onstructorAccessorImpl.java:45)
在 java.lang.reflect.Constructor.newInstance(Constructor.java:525)
在 java.lang.Class.newInstance0(Class.java:372)
在 java.lang.Class.newInstance(Class.java:325)
在 sun.applet.AppletPanel.createApplet(AppletPanel.java:795)
在 sun.applet.AppletPanel.runLoader(AppletPanel.java:724)
在 sun.applet.AppletPanel.run(AppletPanel.java:378)
在 java.lang.Thread.run(Thread.java:722)

代码:

import javax.swing.*;
import java.awt.event.*;
import java.net.URL;
import java.applet.*;
import java.awt.*;

public class Lab5b extends JApplet {
  private AudioClip audioClip;

  public Lab5b() {
    add(new ImagePanel());

    URL urlForAudio = getClass().getResource("audio/us.mid");
    audioClip = Applet.newAudioClip(urlForAudio);
    audioClip.loop();
  }

  public void start() {
    if (audioClip != null) audioClip.loop();
  }

  public void stop() {
    if (audioClip != null) audioClip.stop();
  }

  /** Main method */
  public static void main(String[] args) {
    // Create a frame
    JFrame frame = new JFrame("Lab 5");

    // Create an instance of the applet
    Lab5b applet = new Lab5b();
    applet.init();

    // Add the applet instance to the frame
    frame.add(applet, java.awt.BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Display the frame
    frame.setSize(200, 660);
    frame.setVisible(true);
  }
}

class ImagePanel extends JPanel {
  private ImageIcon imageIcon = new ImageIcon("image/us.gif");
  private Image image = imageIcon.getImage();
  private int y = 550;

  public ImagePanel() {
        Timer timer = new Timer(120, new TimerListener());
        timer.start();
    }

    class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            increaseY();
        }
    }


  public void increaseY() {
        if (y > 0) {
            y--;
            repaint();
        }
    }

  /** Draw image on the panel */
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (image != null) {
      g.fillRect(0, 0, 10, 660);
      g.drawImage(image, 11, y, 160, 84, this);
        }
  }
}

【问题讨论】:

  • Eclipse 中的小程序查看器。
  • Lab5b 类是否在默认包中? getResource 解析相对于类的资源,因此您可能希望使用“/audio/us.mid”作为路径。
  • 音频剪辑似乎不在您期望的位置
  • @LeonardBrünings - Lab5b 位于默认包中。对路径的更改没有帮助。此外,当小程序查看器打开时,窗口底部会显示一条消息,说明 Start:Applet Not initialized。
  • 你能给我们你的目录布局吗?对于根项目目录中的 windows dir /s /b

标签: java


【解决方案1】:

getResource方法在找不到资源时返回null,这使得AppletAudioClip构造函数抛出异常。确保us.mid 位于名为audio 的目录中,该目录与Lab5b.class 位于同一目录中:

  • Lab5b.class
  • 音频/
    • us.mid

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多