GetCodeBase() and GetDocumentBase() return null

Hello i have a liite problem.

When i try to use getDocumentBase() or getCodeBase() the stub of the applet is null so i get a nullpointer exception. I've maid a very basic JApplet that just calls getDocumentBase() and prints the result.

I've tried the following applet and html (placed applet and html in the same folder, opening html file with IE or FireFox and i see that the applet is not loaded, the error from the java console is:

java.lang.NullPointerException

at java.applet.Applet.getDocumentBase(Unknown Source)

at MyPackage.NewJApplet.<init>(NewJApplet.java:10)

Here is the code:

jar:

public class NewJApplet extends javax.swing.JApplet

{

/** Creates a new instance of NewJApplet */

public NewJApplet()

{

String temp = getDocumentBase().toString();

System.out.println(temp);

}

}

html:

<html>

<head>

<title></title>

</head>

<body>

<Applet code="MyPackage.NewJApplet.class" archive="JavaApplication12.jar" width="600" height="400">

</body>

</html>

[1178 byte] By [Nilsqsfdgfgqsdfgsda] at [2007-11-26 17:39:49]
# 1

You can't call getDocumentRoot() in the constructor of an Applet. It hasn't been set yet. Instead, call it in the init() method:

public class NewJApplet extends JApplet {

public void init() {

String temp = this.getDocumentBase().toString();

System.out.println( temp );

}

/** Creates a new instance of NewJApplet */

public NewJApplet() {

}

}

morrisnka at 2007-7-9 0:07:59 > top of Java-index,Java Essentials,Java Programming...
# 2
Yeah, think about what the lifecycle of an applet has to be:1) the browser instantiates it2) the browser adds the stub3) the browser starts invoking standard methods (init, start)etc.
paulcwa at 2007-7-9 0:07:59 > top of Java-index,Java Essentials,Java Programming...
# 3
thank you very much, i've tested this and it is indeed the solution to my problem.
Nilsqsfdgfgqsdfgsda at 2007-7-9 0:07:59 > top of Java-index,Java Essentials,Java Programming...