what is wrong?(simple applet q)

I got runtime exception, why?  I have to add a no arg contructor to it. why?

import java.applet.Applet;
import java.awt.*;

public class Sample extends Applet {

private String text = "Hello World";

public void init() {
add(new Label(text));
}

public Sample (String string) {
text = string;
}
}
embertAsked:
Who is Participating?
 
sen_kumConnect With a Mentor Commented:

   Because when an applet is started the jvm looks for a constructor without no arguments to make an instance of the applet. If no Constructor is found then jvm call the default constructor. If some other constructor is present then jvm excepts constructor.

Now the program does not needs a constructor without any argument.

import java.applet.Applet;
import java.awt.*;

public class Sample extends Applet {

         private String text = "Hello World";

         public void init() {
             setString( "xyz" ) ;
             add(new Label(text));
         }

        public void setString (String string) {
                      text = string;
        }
}
0
All Courses

From novice to tech pro — start learning today.