Deadrow
Deadrow

Reputation: 11

java applet ExceptionInInitializerError

I'm new to programming with Applets and wanted to make an Applet to put on a website. So here we go.

The Goal of this project was that if you click the button, it will open a JFrame on top of the browser. but while testing, it gives me a java.lang.ExceptionInInitializerError. This is the source code:

public class LaunchMenu extends Applet {

    public static LoginScreen login;
    public static Game game;
    public JButton button;
    public void init() {
        try {button= new JButton("Press this button to start");
        add(button);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                login = new LoginScreen();

            }
        });
        } catch (Exception e) {
            e.getMessage();
            e.getCause();
        }
    }

    public void start() {
        login = new LoginScreen();
    }

    public void stop() {
        login.dispose();
        game.stop();
    }

    public static void main(String[] args) {

    }

}

note: It works in Eclipse with it's Applet window, but not on the website.

edit:

Exception in thread "AWT-EventQueue-2" java.lang.IllegalStateException: Applet's parent container not set up
    at sun.plugin2.applet.Plugin2Manager.start(Unknown Source)
    at sun.plugin2.main.client.PluginMain$StartAppletRunner.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(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$1.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)
CacheEntry[http://localhost/AppletTest/Applet.jar]: updateAvailable=true,lastModified=Mon Apr 13 12:24:52 CEST 2015,length=5051938

Upvotes: 0

Views: 1230

Answers (1)

Deadrow
Deadrow

Reputation: 11

Ok I fixed my problem. Here's the solution that worked for me.

SourceCode stays the same (except for a minor change)* :

public class LaunchMenu extends Applet {

    public static LoginScreen login;
    public static Game game;
    public JButton button;
    public void init() {
        try {button= new JButton("Start the game");
        add(button);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                login = new LoginScreen();
                login.setVisible(true); *

            }
        });
        } catch (Exception e) {
            e.getMessage();
            e.getCause();
        }
    }

    public void start() {
        login = new LoginScreen();
    }

    public void stop() {
        login.dispose();
        game.stop();
    }

    public static void main(String[] args) {

    }
}

But I added a java.policy file in the same folder as the Applet. Within this file I wrote the following code:

grant { 
      permission java.security.AllPermission; 
}; 

After this was setup, I ran in another problem named java.lang.RuntimePermission: "exitVM.0"

The solution to this problem was simple. In the class with my JFrame, in my case LoginScreen, there was a line code setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);. Delete this or comment it, this worked for me and now my Applet is visible and the Login frame opens. which was all I needed.

Hope this helps alot of people with the same problem.

Upvotes: 1

Related Questions