Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
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 1191 times)
0 Members and 1 Guest are viewing this topic.
Offline Ultroman

JGO Knight


Medals: 24
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: 605
Projects: 4
Exp: 16 years


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
Offline Ultroman

JGO Knight


Medals: 24
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: 605
Projects: 4
Exp: 16 years


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
Offline Ultroman

JGO Knight


Medals: 24
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: 605
Projects: 4
Exp: 16 years


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
Offline Ultroman

JGO Knight


Medals: 24
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: 605
Projects: 4
Exp: 16 years


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
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!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


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
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (24 views)
2014-04-15 18:08:23

BurntPizza (19 views)
2014-04-15 03:46:01

UprightPath (33 views)
2014-04-14 17:39:50

UprightPath (17 views)
2014-04-14 17:35:47

Porlus (33 views)
2014-04-14 15:48:38

tom_mai78101 (59 views)
2014-04-10 04:04:31

BurntPizza (117 views)
2014-04-08 23:06:04

tom_mai78101 (217 views)
2014-04-05 13:34:39

trollwarrior1 (184 views)
2014-04-04 12:06:45

CJLetsGame (191 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!