Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java Applet question

Reply
Thread Tools

Java Applet question

 
 
yingjian.ma1955@gmail.com
Guest
Posts: n/a
 
      04-25-2006
I have a Java applet called TestColor that does not work. Here is the
code.

TestColor.java code:

import java.awt.*;
import java.applet.*;
public class TestColor extends Applet {
String s1;
public void init() {
Color c;
s1 = getParameter("mycolor");
if (s1.equals("blue"))
c = Color.blue;
else if (s1.equals("red"))
c = Color.red;
else if (s1.equals("green"))
c = Color.green;
else
c = Color.cyan;
setBackground(c); }}

Html code:

<HTML>
<HEAD>
<TITLE>A Simple Program</TITLE>
</HEAD>
<BODY>
<h2>Here is the applet:</h2><br>
<APPLET CODE="TestColor.class" WIDTH=550 HEIGHT=500 alt="white">
<param name="mycolor" value="blue">
Sorry, you aren't running a Java-capable browser.
</APPLET>
</BODY>
</HTML>

If I change mycolor to adjective1 in both files. It works. Could you
kindly try it and tell me why?

When I run it in a debugger, I got this msg:

java.lang.NullPointerException
at TestColor.init(TestColor.java:9)
at sun.applet.AppletPanel.run(AppletPanel.java:37
at java.lang.Thread.run(Thread.java:595)
Warning: classic VM not supported; client VM will be used.

What is the bug?

Thanks a lot.

 
Reply With Quote
 
 
 
 
Amfur Kilnem
Guest
Posts: n/a
 
      04-25-2006

<> wrote in message
news: oups.com...
>I have a Java applet called TestColor that does not work. Here is the
> code.
>
> TestColor.java code:
>
> import java.awt.*;
> import java.applet.*;
> public class TestColor extends Applet {
> String s1;
> public void init() {
> Color c;
> s1 = getParameter("mycolor");
> if (s1.equals("blue"))
> c = Color.blue;
> else if (s1.equals("red"))
> c = Color.red;
> else if (s1.equals("green"))
> c = Color.green;
> else
> c = Color.cyan;
> setBackground(c); }}
>
> Html code:
>
> <HTML>
> <HEAD>
> <TITLE>A Simple Program</TITLE>
> </HEAD>
> <BODY>
> <h2>Here is the applet:</h2><br>
> <APPLET CODE="TestColor.class" WIDTH=550 HEIGHT=500 alt="white">
> <param name="mycolor" value="blue">
> Sorry, you aren't running a Java-capable browser.
> </APPLET>
> </BODY>
> </HTML>
>
> If I change mycolor to adjective1 in both files. It works. Could you
> kindly try it and tell me why?
>
> When I run it in a debugger, I got this msg:
>
> java.lang.NullPointerException
> at TestColor.init(TestColor.java:9)
> at sun.applet.AppletPanel.run(AppletPanel.java:37
> at java.lang.Thread.run(Thread.java:595)
> Warning: classic VM not supported; client VM will be used.
>
> What is the bug?


You are not checking the return value of getParameter(). If "mycolor" isn't
found, the returned value will be null, and the very next line will cause
the null pointer exception.

As to why it appears not to be working with "mycolor", maybe you have a
stale applet and/or HTML in your browser...?



 
Reply With Quote
 
 
 
 
yingjian.ma1955@gmail.com
Guest
Posts: n/a
 
      04-25-2006
Thanks.

I tried it on another PC. It works. So it is my PC's problem. The
two PCs have XP with IE6 and Java 1.5. But the working one has AMD
cpu. The other has Intel cpu. Here is the problem with this "bad" PC.


If I use the word such as mycolor, it displays a gray box. When I move
the vertical scroll bar, the blue color flashes.
If I use the word adjective1, it displays a blue box, which is correct.

I cleaned the catch already. What can I do to fix it?

 
Reply With Quote
 
yingjian.ma1955@gmail.com
Guest
Posts: n/a
 
      04-25-2006
Sorry, it is not the word causing the problem. The code is different.
The original code works. Here is the code that has problem:

import javax.swing.*;
import java.awt.*;
public class TestColor extends JApplet {
String s1;
public void init() {
Color c;
s1 = getParameter("mycolor");
if (s1.equals("blue"))
c = Color.blue;
else if (s1.equals("red"))
c = Color.red;
else if (s1.equals("green"))
c = Color.green;
else
c = Color.cyan;
setBackground(c); }}

So it looks like JApplet does not work in this code.

 
Reply With Quote
 
Dag Sunde
Guest
Posts: n/a
 
      04-25-2006
<> skrev i melding
news: oups.com...
> Sorry, it is not the word causing the problem. The code is different.
> The original code works. Here is the code that has problem:
>
> import javax.swing.*;
> import java.awt.*;
> public class TestColor extends JApplet {
> String s1;
> public void init() {
> Color c;
> s1 = getParameter("mycolor");
> if (s1.equals("blue"))
> c = Color.blue;
> else if (s1.equals("red"))
> c = Color.red;
> else if (s1.equals("green"))
> c = Color.green;
> else
> c = Color.cyan;
> setBackground(c); }}
>
> So it looks like JApplet does not work in this code.


You say both PC's have Java 1.5, but do both have the Java *plugin*
installed for IE?

And if so, is "Use Sun Java plugin" enabled in IE on both PS's?

--
Dag.


 
Reply With Quote
 
yingjian.ma1955@gmail.com
Guest
Posts: n/a
 
      04-26-2006
The 3rd post was not correct. Both PCs have the same behavior. The
Applet class works and the JApplet class does not work. Could you try
it to tell me how to fix it?

Thanks.

 
Reply With Quote
 
Amfur Kilnem
Guest
Posts: n/a
 
      04-26-2006

<> wrote in message
news: ups.com...
> The 3rd post was not correct. Both PCs have the same behavior. The
> Applet class works and the JApplet class does not work. Could you try
> it to tell me how to fix it?
>
> Thanks.
>


Try this:

import java.awt.*;
import javax.swing.*;
public class TestColor extends JApplet {
String s1;
public void init() {

Container contentPane = getContentPane();
Color c;
s1 = getParameter("mycolor");
if (s1.equals("blue"))
c = Color.blue;
else if (s1.equals("red"))
c = Color.red;
else if (s1.equals("green"))
c = Color.green;
else
c = Color.cyan;
contentPane.setBackground(c);
}
}




 
Reply With Quote
 
yingjian.ma1955@gmail.com
Guest
Posts: n/a
 
      04-26-2006
It works. Thank you very much.

Could you tell me why my JApplet did not work?

 
Reply With Quote
 
Amfur Kilnem
Guest
Posts: n/a
 
      04-26-2006

<> wrote in message
news: oups.com...
> It works. Thank you very much.
>
> Could you tell me why my JApplet did not work?
>


In a JApplet, everything must be put into the contentPane, which obscures
the underlying Applet.



 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      04-27-2006

<> wrote in message
news: ups.com...
> The 3rd post was not correct. Both PCs have the same behavior. The
> Applet class works and the JApplet class does not work. Could you try
> it to tell me how to fix it?


It sounds like you're misunderstanding how parameters work. Try reading
through http://java.sun.com/docs/books/tutor...let/param.html

- Oliver

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java.applet.Applet.getParameter() blaine@worldweb.com Java 4 01-11-2007 08:04 PM
confussed about showStatus in java.applet.Applet yawnmoth Java 1 08-15-2006 05:44 AM
Java Applet loading in Applet Viewer but not in HTML page Archana Java 1 10-24-2004 11:41 PM
Java applet failed when I try to load the avi file in my java applet Krista Java 3 09-15-2004 02:53 AM
Re: play wave files using java.applet.Applet webster Java 0 07-20-2003 01:51 PM



Advertisments