I can't get the sound to play when I click on the "Play" Button using Java Eclipse I have Tried many videos but nothing is working I have the Project set up so when you click what sound you want to use it opens a smaller window with play, pause, and stop buttons. I am also using Java WindowBuilder:Swing designer. the path to the image is not the problem as i have also used a direct path and that didn't work either. I am very Confused as I am new to programming and there are no errors with the code.
Here is the Code:
package com.dashboard.rickroll;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class RickRoll {
private JFrame frame;
String RR;
ButtonHandler bhandler = new ButtonHandler();
RRSound RRS = new RRSound();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RickRoll window = new RickRoll();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
new RickRoll();
}
public RickRoll() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setTitle("SoundBoard pre-Alpha v 0.01");
frame.setBackground(Color.WHITE);
frame.getContentPane().setBackground(Color.WHITE);
frame.getContentPane().setLayout(null);
JButton Playbtn = new JButton("Play");
Playbtn.setBounds(10, 150, 60, 50);
Playbtn.setFocusPainted(false);
Playbtn.addActionListener(bhandler);
frame.getContentPane().add(Playbtn);
JLabel RickRollTtl = new JLabel("Rick Roll");
RickRollTtl.setFont(new Font("Times New Roman", Font.PLAIN, 40));
RickRollTtl.setBounds(66, 11, 168, 41);
frame.getContentPane().add(RickRollTtl);
JButton Pause = new JButton("Pause");
Pause.setBounds(110, 150, 70, 50);
frame.getContentPane().add(Pause);
JButton Stop = new JButton("Stop");
Stop.setBounds(224, 150, 60, 50);
frame.getContentPane().add(Stop);
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
RR = ".//Sounds//RickRoll.wav";
}
public class RRSound{
Clip clip;
public void setFile(String soundFileName) {
try {
File RickRoll = new File(soundFileName);
AudioInputStream sound = AudioSystem.getAudioInputStream(RickRoll);
clip = AudioSystem.getClip();
clip.open(sound);
} catch(Exception e) {
}
}
public void play() {
clip.setFramePosition(0);
clip.start();
}
}
public class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent event) {
RRS.setFile(RR);
RRS.play();
}
}
public void setVisible() {
}
}
Whenever I run the program and click play, I get these errors in the Console:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.dashboard.rickroll.RickRoll$RRSound.play(RickRoll.java:96)
at com.dashboard.rickroll.RickRoll$ButtonHandler.actionPerformed(RickRoll.java:107)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)