Reputation: 21
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)
Upvotes: 0
Views: 89
Reputation: 7910
It's helpful if you quote the line being cited in the console! Counting is not easy, especially with large files and large spaces (I think they are 3 blank lines in length at some spots but not sure).
Is line 96 the following?
clip.setFramePosition(0);
One thing that will help is to have the try/catch in the setFile() method print the call stack if an exception is called. As it stands, there is no way to know if an exception occurred in this code block. The console message says that there was a NPE, so since clip
is the only object on that line, clip
must be null. This implies that something went wrong in the instantiation of that reference variable. The instantiation occurs in the try/catch. Any diagnostic messages that might help are lost.
Thus the line e.printStackTrace();
at the least should be included in your catch
code block.
I think that it's generally better to use URL
as the argument than Form
for the line AudioInputStream sound = AudioSystem.getAudioInputStream(RickRoll);
A couple examples of this usage (using URL) can be seen at the StackOverflow About Javasound page. Unfortunately the external address used in the Clip example is a dead link. But if you had a valid http address for a wav file, this would work. However, the lines for instantiating the AudioInputStream in the SourceDataLine example remain valid, and can be used to instantiate the AudioInputStream for a Clip. Note the info in the comments about folder locations!
If you alter your code to follow that (AND put in the stack trace in your catch
block), and still get puzzling errors, feel free to Edit/Update your post with the new info. We will try to help further.
By the way, it is kind of important to use standard conventions when naming variables. You should start your variables with a lower case letter. Most Java code readers would tend to assume that if something starts with an upper case letter, it is a Class name--so when it is not that, it can be confusing.
Upvotes: 2