I have an applet that is not initializing:

import javax.swing.*;


public class WordSearchApplet extends JApplet
{        
    public WordSearchApplet (int width, int height)
    {
    }
    
    
    public void init ()
    {
        new WordSearchApplet (700, 800);
    }
}

Here is the error I get.

load: WordSearchApplet can't be instantiated.
java.lang.InstantiationException: WordSearchApplet
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

When I change it so the constructor takes no parameters, it works:

import javax.swing.*;


public class WordSearchApplet extends JApplet
{        
    public WordSearchApplet ()
    {
    }
    
    
    public void init ()
    {
        new WordSearchApplet ();
    }
}

Here is my html file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Word Search Applet</title>
</head>
    <body>
         <applet code="WordSearchApplet" archive="WordSearchList.jar" name="Word Search" width="700" height="900" id="Word Search"></applet>
    </body>
</html>

Can you not pass parameters from the init () function to the applet's constructor?

Recommended Answers

All 5 Replies

you can pass parameters using

<applet .....
<PARAM name="param" value="value">
</applet>

in your init

String param = getParameter("param");

just do conversion to int, but you are specify width and height already, but thats how pass them

you can pass parameters using

<applet .....
<PARAM name="param" value="value">
</applet>

in your init

String param = getParameter("param");

just do conversion to int, but you are specify width and height already, but thats how pass them

O.K. Cool. So that's how I pass them from the html page to init, but do I not also need to pass them from init to the constructor, as in:

import javax.swing.*;


public class WordSearchApplet extends JApplet
{
    int appletHeight;
    int appletWidth;


    public WordSearchApplet (int width, int height)
    {
        appletWidth = width;
        appletHeight = height;
    }
    
    
    public void init ()
    {
        String stringWidth = getParameter("width");
        String stringHeight = getParameter("height");
        int width = Integer.valueOf (stringWidth);
        int height = Integer.valueOf (stringHeight);
        new WordSearchApplet (width, height);
    }
}
<applet code="WordSearchApplet"
          archive="WordSearchList.jar"
          name="Word Search"
          width="700" height="900"
          id="Word Search">
         <PARAM name="width" value="700">
         <PARAM name="height" value="900">
         </applet>

I still get the same error. I don't think the problem is passing the parameters from the html page to the applet's init function, but rather passing the parameters from the init function to the constructor.

Applets shouldn't have constructors because an applet isn't guaranteed to have a full environment until init is called. So the code you would normally put in your constructor should go into the init() method.

Also, things such as width and height of an are automagically pulled from the HTML for you and can be accessed via getWidth() and getHeight() .

public void init ()
    {
        String stringWidth = getParameter("width");
        String stringHeight = getParameter("height");
        int width = Integer.valueOf (stringWidth);
        int height = Integer.valueOf (stringHeight);
        new WordSearchApplet (width, height);
    }
}

Also, keep in mind that an instance of your WordSearchApplet has already been created when you get here— init() is an instance method.

Cool. Thank you. It's working now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.