Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java & JavaScript Applet error

Posted on 2004-09-27
13
Medium Priority
?
385 Views
Last Modified: 2013-12-16
I was trying to create an applet and I have some problems.

Here is the code:

// File: sleepclass.java
public class sleepclass
{
  public void sleep(float timeInMillis) throws InterruptedException
  {
    Thread.sleep((long)timeInMillis);
  }
}

// File: test.htm
<html>
<head></head>
<body>
<APPLET CODE="sleepclass.class" WIDTH=1 HEIGHT=1></applet>
<script language="javascript">
   window.status  = "abc";
   window.document.applets[0].sleep(2000);
   window.status += "def";
</script>
</body>
</html>


Then, I compile sleepclass.java:

c:\> javac sleepclass.java


When I launch test.htm, I get a JavaScript error in line 'window.document.applets[0].sleep(2000);'


Some idea?
0
Comment
Question by:Eligio
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
13 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 12161000
public class sleepclass extends java.applet.Applet

0
 
LVL 35

Expert Comment

by:TimYates
ID: 12161013
you also probably want to fire the javascript when a button is pressed, etc...  As the applet will probably load into the browser AFTER the javascript has run...
0
 

Author Comment

by:Eligio
ID: 12161111
Thanks TimYates, but the problem continues. Now I have:

// File: sleepclass.java
public class sleepclass extends java.applet.Applet
{
  public void sleep(float timeInMillis) throws InterruptedException
  {
    Thread.sleep( (long) timeInMillis );
  }
}

// File: test.htm
<html>
<head>
<script language="javascript">
  function fncInit () {
    window.status  = "abc";
    window.document.applets[0].sleep(2000);
    window.status += "def";
  };
</script>
</head>
<body >
<APPLET CODE="sleepclass.class" WIDTH=100 HEIGHT=10></applet>
<input type="button" onclick="fncInit();">
</body>
</html>


But error is the same. I let the applet to finish loading and then I press the button, but I always get a JS error. The error description (traslated from spanish) is: Object doesn't accept this property or method.

Thanks
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:TimYates
ID: 12161178
<html>
<head>
<script language="javascript">
  function fncInit () {
    window.status  = "abc";
    document.getElementById( 'VA' ).sleep( 2000 ) ;
    window.status += "def";
  };
</script>
</head>
<body >
  <applet id="VA" width="1" height="1" code="sleepclass" MAYSCRIPT>
  </applet>
  <input type="button" onclick="fncInit();">
</body>
</html>
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12161284
If that doesn't work, try this for your applet (get rid of the "throws")

public class sleepclass extends java.applet.Applet
{
  public void sleep( int timeInMillis )
  {
    try { Thread.sleep( timeInMillis ); } catch( InterruptedException ex ) {}
  }
}

also, why were you passing "float" to this method?
0
 

Author Comment

by:Eligio
ID: 12161539
TimYates, same error.

I used float beacause I read that javascript numeric arguments are always catched by the applet as floats, and then the call must be with a long ones.

Don't know what happends, beacause if I try to write a more simple applet, it always throws the same error.

public class A extends java.applet.Applet
{
  public void B( ) { //none }
}

When I do:
document.applets[0].B()
or
document.applets("my_applet_name").B()
or
document.my_applet_name.B()
or
document.getElementById(my_applet_id).B()

always get an error.

If I do: alert(document.applets[0].id), VA is printed, so I supose the applet is well referenced.


Any idea?


Thanks
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12161591
Try...

What browser are you using?

Tim

-----------------

// File: sleepclass.java
public class sleepclass extends java.applet.Applet
{
  public String woo()
  {
    return "woo!" ;
  }
}

// File: test.htm
<html>
<head>
<script language="javascript">
  function fncInit () {
    window.status  = "abc";
    alert( window.getElementById( "VA" ).woo() ) ;
    window.status += "def";
  };
</script>
</head>
<body >
<APPLET id="VA" CODE="sleepclass" WIDTH=100 HEIGHT=10 MAYSCRIPT></applet>
<input type="button" onclick="fncInit();">
</body>
</html>
0
 

Author Comment

by:Eligio
ID: 12161624
One more thing...

I have edited sleepclass.java, and add this code:

// File: sleepclass.java
public class sleepclass extends java.applet.Applet
{
  public void init()
  {
    System.out.println("We have been initialized");
  }
      
  public void sleep( int timeInMillis )
  {
    try { Thread.sleep( timeInMillis ); } catch( InterruptedException ex ) {}
  }
}

If I run: appletviewer test.htm, I can see "We have been initialized" on OS symbol.

But when I try to call the function ... crash!

Maybe this help you.


Thanks.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12161657
You do have the java pluing installed don't you?

Again, what browser are you using?  On what OS?

can you open the java console?  does that print "We have been initialised" when you open the webpage in your browser?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12161682
And...what JDK are you using to compile the applet?
0
 
LVL 35

Accepted Solution

by:
TimYates earned 2000 total points
ID: 12161690
And what version of the plugin do you have installed in your browser?
0
 

Author Comment

by:Eligio
ID: 12161758
Thanks TimYates.

After testing the the applet in other computer it worked. Then I reinstalled java and now it works fine too.



0
 
LVL 35

Expert Comment

by:TimYates
ID: 12162262
Yay!! :-)

Good luck with it all! :-)

Tim
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Geo-targeting is the practice of distributing content based on a person’s location, as best as you can determine it. Let’s look at some ways you could successfully use this tactic. The following tips and case studies could lead to meaningful results.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question