Hey guys,
I am new to Java and am working on a fun little program. It's kinda pointless, but its to teach me the concepts.
Anyway, This is my code:
import javax.swing.*; import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.URL; import java.net.*; public class TestClass extends JFrame implements ActionListener { AudioClip clip; public static void main(String[] args) { new TestClass(); } private JButton button_ok; public TestClass() { this.setSize(300,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Peanut Butter Jelly Time!"); URL url = TestClass.class.getResource("PBnJ.wav"); AudioClip clip = Applet.newAudioClip(url); clip.loop(); this.setVisible(true); } JPanel panel = new JPanel(); { button_ok = new JButton("Shut up Mr.Banana!"); button_ok.addActionListener(this); JLabel label = new JLabel("PEANUT BUTTER JELLY TIME"); panel.add(label); panel.add(button_ok); ImageIcon pic = new ImageIcon("PBn'J.gif"); panel.add(new JLabel(pic)); this.add(panel); this.setVisible(true); } { } private int clickCount = 0; public void actionPerformed(ActionEvent e) { if (e.getSource() == button_ok) { clickCount++; if (clickCount == 1) button_ok.setText("PB n' J Forever!"); else button_ok.setText("Clicking me " + clickCount + " times wont change my answer!"); } { if (clickCount == 10) button_ok.setText("Y U NO LIKE PBn'J?"); } { if (clickCount == 15) button_ok.setText("You cant fight it."); } { if (clickCount == 30) button_ok.setText("IM A BANANA"); } } }
I want the output of the program to be a button that changes the text of it when you click it, a dancing banana, a label, and Peanut Butter Jelly Time Playing in the background. Currently it does everything but the last.
In eclipse, it gives out no errors until I try to run it.
When I run it in Eclipse I get everything but the music. It acts as if the code isnt there, but it gives this error message.
"Exception in thread "main" java.lang.NullPointerException
at sun.applet.AppletAudioClip.<init>(Unknown Source)
at java.applet.Applet.newAudioClip(Unknown Source)
at TestClass.<init>(TestClass.java:31)
at TestClass.main(TestClass.java:16)"
Line 16 is: new TestClass();
Line 31 is: AudioClip clip = Applet.newAudioClip(url);
Please help a new guy out!
-Duster