how call javascript from applet
how call javascript from applet
original java code:
jabaco
html:
it gives Nullpointer exeption error
Why?
original java code:
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.applet.*;
import java.net.*;
public class InJava4 extends Applet{
public void init(){
String msg = "Hello from Java (using javascript alert)";
try {
getAppletContext().showDocument (new URL("javascript:doAlert(\"" + msg +"\")"));
}
catch (MalformedURLException me) {
}
}
}
|
jabaco
|
|
Source code |
1 |
Applet1.Parent.getAppletContext().showDocument(New java#net#URL("javascript:DOalert(\"" + msg +"\")") )
|
html:
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>
Jabaco-Applet
</title>
<SCRIPT>
function DOalert(s) {
alert(s);
}
</SCRIPT>
</head>
<body>
<APPLET ARCHIVE="test5.jar" CODE="Applet1.class" MAYSCRIPT WIDTH="400" HEIGHT="300">
<PARAM NAME="Compiler" VALUE="Jabaco">
</APPLET>
</body>
</html>
|
it gives Nullpointer exeption error
Why?
To find the error, you should add extra debugging statements.
It is not fully clear to me what you intend to achieve with your Java code. Is that a working original sample you intend to port to Jabaco?
A null pointer exception usually happens if either a variable is not properly initialized or an object creation failed for some reason.
To pin-down what has happened, you should trace the code execution and narrow down the area of failure.
Add code to your catch() part to actually get more information.
You are attempting four or more nested operations in a single line of code.
To isolate the error, you could spread this out to several lines and check for success at every step.
Applets on their own are difficult to trace.
It might be a good idea to try parts of your code in a stand-alone Jabaco module.
This test code should give some valuable hints:
Look here for additional help.
Please post your findings
Greetings
A1880
It is not fully clear to me what you intend to achieve with your Java code. Is that a working original sample you intend to port to Jabaco?
A null pointer exception usually happens if either a variable is not properly initialized or an object creation failed for some reason.
To pin-down what has happened, you should trace the code execution and narrow down the area of failure.
Add code to your catch() part to actually get more information.
You are attempting four or more nested operations in a single line of code.
To isolate the error, you could spread this out to several lines and check for success at every step.
Applets on their own are difficult to trace.
It might be a good idea to try parts of your code in a stand-alone Jabaco module.
This test code should give some valuable hints:
|
|
Jabaco Source |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Option Explicit Public Sub Command1_Click() Dim s As String Dim msg As String = "Hello!" Dim url As java#net#URL ' Chr(34) is a trick to state quote characters in strings s = "javascript:DOalert(" & Chr(34) + msg + Chr(34) & ")" Debug.Print s url = New java#net#URL(s) Debug.Print url.toString End Sub |
Look here for additional help.
Please post your findings
Greetings
A1880
This post has been edited 3 times, last edit by "A1880" (Feb 18th 2011, 3:44pm)
The following applet shows how to pass information between applet and javascript in both ways.
The applet Jabaco source:
The HTML page with the embedded applet:
I've used Firefox 3.6 and Java 1.6.0_24
Greetings
A1880
The applet Jabaco source:
|
|
Jabaco Source |
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 30 31 32 33 34 35 36 37 38 39 40 |
Option Explicit ' the applet has a Button "cmdSetLocal" Public Sub cmdSetLocal_Click() ' copy text of textbox "tbLocalInput" SetText Me.tbLocalInput.Text End Sub ' the applet has a button "cmdSetRemote" Public Sub cmdSetRemote_Click() Dim s As String Dim msg As String Dim url As java#net#URL On Error Goto ErrHandler ' take text of "tbLocalInput" msg = Me.tbLocalInput.Text ' Chr(34) is a trick to state quote characters in strings s = "javascript:SetText(" & Chr(34) + msg + Chr(34) & ")" url = New java#net#URL(s) Me.Parent.getAppletContext().showDocument(url) Exit Sub ErrHandler: trace "Error: " & Err.getMessage() End Sub ' sub called from javscript Public Sub SetText(txt As String) Me.lblText.Caption = txt End Sub Private Sub trace(s As String) Me.lblText.Caption = Me.lblText.Caption & vbCrLf & s End Sub |
The HTML page with the embedded applet:
|
|
Source code |
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 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>
Jabaco-Applet
</title>
<SCRIPT LANGUAGE="JavaScript">
function button_clicked (form) {
var TestVar = document.myform.inputbox.value;
document.jsalert.SetText(TestVar);
}
function SetText(s) {
document.myform.inputbox.value = s;
}
</SCRIPT>
</head>
<body>
<APPLET NAME="jsalert" ARCHIVE="jsalert.jar" CODE="Applet1.class" MAYSCRIPT WIDTH="400" HEIGHT="300">
<PARAM NAME="Compiler" VALUE="Jabaco">
</APPLET>
<FORM NAME="myform" ACTION="" METHOD="GET">Enter something in the box: <BR>
<INPUT TYPE="text" NAME="inputbox" VALUE=""><P>
<INPUT TYPE="button" NAME="button" Value="Click" onClick="button_clicked(this.form)">
</FORM>
</body>
</html>
|
I've used Firefox 3.6 and Java 1.6.0_24
Greetings
A1880
Similar threads
-
Tips, Tricks, Samples & Tutorials »-
ORPG in Jabaco-applet
(Apr 19th 2010, 11:55pm)
-
Allgemeine Themen, Fragen und Diskussionen »-
Neue Controls
(Jan 27th 2011, 5:19pm)
-
Allgemeine Themen, Fragen und Diskussionen »-
Security Meldung
(Sep 15th 2009, 9:09pm)
-
General topics, questions and discussions »-
how to implement mouse and keyboard events in an Applet?
(Mar 15th 2009, 12:43pm)
