Simple JAVA / JavaScript parameter problem. getParameter returns null?!

EDIT: Nevermind…one typo and the discovery of a retard-moment fixed everything. Thanks to anyone who spent their time reading this.

Hi.

I’m trying to do something very simple. I want to have an applet running a server, and whenever I receive communications on this server, I want the applet to run a JavaScript method, with a number I get from another server, and show this new number in the browser.

It has been simplified severely, by leaving out all server-code, and for now I just run my update method in the applet constructor. Furthermore I’ve set a fixed number for the update method, instead of the call to the remote server.

The problem I’m having, is that no matter what I do, getParameter cannot find my defined parameter “myparam”, and JSObject is always null. I’ve looked at tonnes of examples and threads with the same problem, but to no avail. Can anyone tell me what I’m doing wrong?

HTML


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function jsUpdateFunction(number){
	alert("Received number: " + number);
}
</script>
</head>
<body>
<applet id="CounterApplet" code="dk/cbit/counterApplet/CounterApplet.class" archive="CounterApplet.jar" MAYSCRIPT>
	<param name="myparam" value="jsUpdateFunction"/>
</applet>
</body>
</html>

Applet:


import java.applet.Applet;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import netscape.javascript.JSObject;

public class CounterApplet extends Applet {
private JSObject jsObject;

@Override
public void start() {
	if(jsObject == null)jsObject = JSObject.getWindow(this);
	super.start();
}
public CounterApplet(){
	updateJS();
}
private void updateJS() {
	System.out.println("Entering updateJS()");
	String jsCallbackName = getParameter("myParam");
	System.out.println("jsCallbackName: "+jsCallbackName);
	/*
	* Here, the applet will try to connect to a server, to find out
	* which number we've reached. For now, it is replaced by a simple String.
	*/
	String number = "42";
	jsObject.eval(jsCallbackName + "(" + number +  ")");
	}
}

Obviously the constructor of CounterApplet is called before it’s start() method is called.

Therefore jsObject is null when updateJS() is invoked.

Solution: call updateJS in start().

Obviously…well, it’s my first time looking at JavaScript, and definitely the first time I’ve heard of the JSObject, and I think I’ve made 2 applets in total ^^

I made it all work, though. It’s a right pain in the arse to have serversockets in applets. I have to sign the JAR every time I want to test it. Gotta love batch-files…

I said ‘obviously’ because this has nothing to do with applets, it’s your typical NPE, caused by a read from a field before it was written into.

Well, it has something to do with applets, since the knowledge of start() being called after the constructor, would’ve saved me a lot of time. It was a rookie mistake. Thanks for helping, though

I won’t want to sound pedant, but really, it really has nothing to do with applets.

The constructor is the first thing of an instance to be called. All other non-static methods can only be called later. Therefore it is a guarantee that start() can only be called after the constructor was called.

I guess I’ll stop here :persecutioncomplex: :slight_smile:

Well, when you put it that way :slight_smile:

puts on stupid-hat

I guess 8 hours of staring at one thing CAN make you oblivious to the obvious :wink:

In fact that’s not completely true … if the start method ( which is declared in the Applet class or an upper class as suggested by the @override ) was called in the default constructor of Applet, it would have been called before the constructor of CounterApplet :slight_smile:

True, but how often does that happen? Normally such a method would be private, as it’s just bad design to expose it.

sure

how often is not the point, it can or cannot happen ( sorry to be so binary :slight_smile: ) … and as you said “it is a guarantee that start() can only be called after the constructor was called” and you insisted on this point, I just wanted to say that it was a mistake …

… but anyway the issue is fixed and everybody is happy :smiley:

Actually, I stand by that remark:

[quote=“Riven,post:6,topic:40858”]
It was no coincidence that I said it was called in that order, whether it returns in the same other is another matter.