Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simple JAVA / JavaScript parameter problem. getParameter returns null?!  (Read 617 times)
0 Members and 1 Guest are viewing this topic.
Offline Ultroman

JGO Knight


Medals: 23
Projects: 1


Snappin' at snizzes since '83


« Posted 2013-01-28 14:44:13 »

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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
<!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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
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 +  ")");
   }
}

- Jonas
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #1 - Posted 2013-01-28 15:45:34 »

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().

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline Ultroman

JGO Knight


Medals: 23
Projects: 1


Snappin' at snizzes since '83


« Reply #2 - Posted 2013-01-28 18:23:39 »

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...

- Jonas
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #3 - Posted 2013-01-28 18:28:40 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline Ultroman

JGO Knight


Medals: 23
Projects: 1


Snappin' at snizzes since '83


« Reply #4 - Posted 2013-01-28 18:31:00 »

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

- Jonas
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #5 - Posted 2013-01-28 18:45:33 »

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 Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline Ultroman

JGO Knight


Medals: 23
Projects: 1


Snappin' at snizzes since '83


« Reply #6 - Posted 2013-01-28 18:52:59 »

Well, when you put it that way Smiley

*puts on stupid-hat*

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

- Jonas
Offline deathpat
« Reply #7 - Posted 2013-01-28 19:59:58 »

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.

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 Smiley

work in progress : D A E D A L U S
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #8 - Posted 2013-01-28 20:12:36 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline deathpat
« Reply #9 - Posted 2013-01-28 20:57:11 »

Normally such a method would be private, as it's just bad design to expose it.

sure

Quote from: Riven
True, but how often does that happen?

how often is not the point, it can or cannot happen ( sorry to be so binary Smiley ) ... 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 Cheesy

work in progress : D A E D A L U S
Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #10 - Posted 2013-01-28 21:15:24 »

Actually, I stand by that remark:
Therefore it is a guarantee that start() can only be called after the constructor was called.
It was no coincidence that I said it was *called* in that order, whether it *returns* in the same other is another matter.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (83 views)
2013-05-17 21:29:12

alaslipknot (91 views)
2013-05-16 21:24:48

gouessej (122 views)
2013-05-16 00:53:38

gouessej (114 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (187 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.149 seconds with 21 queries.