Question

The following java program gets compiled successfully but when I try to run it using appletviewer, I get the following (in command prompt) with a message "applet not initialized" in the applet window.

import java.applet.*;
import java.awt.*;
/*
<applet code="ParamDemo" width="300" height="300">
<param name="first" value="20">
<param name="second" value="30">
</applet>
*/
class ParamDemo extends Applet
{
int x,y,sum;
public void init()
{
x=Integer.parseInt(getParameter("first"));
y=Integer.parseInt(getParameter("second"));
}
public void paint(Graphics g)
{
sum=x+y;
g.drawString("Total Sum is"+sum,100,100);
}
}

in the Command prompt -

C:\Documents and Settings\Salman\Desktop>javac ParamDemo.java

C:\Documents and Settings\Salman\Desktop>appletviewer ParamDemo.java
load: ParamDemo is not public or has no public constructor.
java.lang.IllegalAccessException: Class sun.applet.AppletPanel can not access a
member of class ParamDemo with modifiers ""
        at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95)
        at java.lang.Class.newInstance0(Class.java:368)
        at java.lang.Class.newInstance(Class.java:327)
        at sun.applet.AppletPanel.createApplet(AppletPanel.java:793)
        at sun.applet.AppletPanel.runLoader(AppletPanel.java:722)
        at sun.applet.AppletPanel.run(AppletPanel.java:379)
        at java.lang.Thread.run(Thread.java:722)

I am unable to figure out the problem ...

Any help would be appreciated !

Was it helpful?

Solution

ParamDemo is not public

indicates you shall write public before class ParamDemo extends ....

If no access modifier (such as public) is given for class, it is only accessible within the same package - no wonder appletviewer has problems with it.

An explicit constructor shouldn't be neccessary.

OTHER TIPS

You need to define the ParamDemo class as public and define a function:

ParamDemo() {
    //Construct your object here.
} 

Within the class ParamDemo.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top