
-----------------------------------
idtt2s
Thu Jan 12, 2006 10:28 pm

Start: Applet not initiated
-----------------------------------
My very simple 33 line applet can't run, giving the error "Start: Applet not initiated"

This is the Console output from Eclipse java.lang.NullPointerException
	at java.applet.Applet.getCodeBase(Applet.java:146)
	at SoundApplet.(SoundApplet.java:11)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
	at java.lang.Class.newInstance0(Class.java:350)
	at java.lang.Class.newInstance(Class.java:303)
	at sun.applet.AppletPanel.createApplet(AppletPanel.java:721)
	at sun.applet.AppletPanel.runLoader(AppletPanel.java:650)
	at sun.applet.AppletPanel.run(AppletPanel.java:324)
	at java.lang.Thread.run(Thread.java:595)


And this is the applet, too simple to really have errors, but I haven't done Java in so long I wouldn't know.


import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class SoundApplet extends JApplet implements ActionListener {
	JButton buttonStart = new JButton("Start");
	JButton buttonStop = new JButton("Stop");
	AudioClip clip = getAudioClip(getCodeBase(), "spacemusic.au");
	
	public void init() {
		buttonStart.setSize(100, 20);
		buttonStop.setSize(100, 20);
		
		buttonStop.addActionListener(this);
		buttonStart.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == buttonStart) {
			clip.play();
		} else if (e.getSource() == buttonStop) {
			clip.stop();
		}
	}
	
	public void paint(Graphics g) {
		getContentPane().add(buttonStart);
		getContentPane().add(buttonStop);
	}
}


1st post question = How do you add syntax colouring in code tags?

-----------------------------------
Hikaru79
Thu Jan 12, 2006 11:21 pm


-----------------------------------
Here you go.

import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.net.MalformedURLException;
import javax.swing.*;

public class SoundApplet extends JApplet implements ActionListener
{
    JButton buttonStart = new JButton ("Start");
    JButton buttonStop = new JButton ("Stop");
    AudioClip clip;

    public void init ()
    {

        try
        {
            AudioClip clip = getAudioClip (new URL ("spacemusic.au"));
        }
        catch (MalformedURLException f)
        {
        }

        buttonStart.setSize (100, 20);
        buttonStop.setSize (100, 20);

        buttonStop.addActionListener (this);
        buttonStart.addActionListener (this);
    }


    public void actionPerformed (ActionEvent e)
    {
        if (e.getSource () == buttonStart)
        {
            clip.play ();
        }
        else if (e.getSource () == buttonStop)
        {
            clip.stop ();
        }
    }


    public void paint (Graphics g)
    {
        getContentPane ().add (buttonStart);
        getContentPane ().add (buttonStop);
    }
}


-----------------------------------
idtt2s
Fri Jan 13, 2006 4:05 pm


-----------------------------------
I think my Eclipse applet is complaining that it doesn't find spacemusic.au. Where should I put spacemusic.au so that Eclipse can see it?

-----------------------------------
Hikaru79
Fri Jan 13, 2006 4:14 pm


-----------------------------------
In the same directory as the build.xml file, I believe. Or you can give the URL a full path to the file. (C:\Program Files\...\etc)

-----------------------------------
idtt2s
Fri Jan 13, 2006 4:18 pm


-----------------------------------
Where is build.xml?

I get 

ming@localhost ~/Desktop/Java Workspace/TestApplets $ ls
java.policy.applet  SoundApplet.java  test.html
SoundApplet.class   spacemusic.au
ming@localhost ~/Desktop/Java Workspace/TestApplets $ pwd
/home/ming/Desktop/Java Workspace/TestApplets

