Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Java Applet question

Thread view: 
yingjian.ma1955@gmail.com - 25 Apr 2006 01:06 GMT
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:378)
    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.
Amfur Kilnem - 25 Apr 2006 02:20 GMT
>I have a Java applet called TestColor that does not work.  Here is the
> code.
[quoted text clipped - 45 lines]
>
> 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...?
yingjian.ma1955@gmail.com - 25 Apr 2006 18:29 GMT
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?
yingjian.ma1955@gmail.com - 25 Apr 2006 18:59 GMT
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.
Dag Sunde - 25 Apr 2006 20:12 GMT
> Sorry, it is not the word causing the problem.  The code is different.
> The original code works.  Here is the code that has problem:
[quoted text clipped - 17 lines]
>
> 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?

Signature

Dag.

yingjian.ma1955@gmail.com - 26 Apr 2006 19:11 GMT
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.
Amfur Kilnem - 26 Apr 2006 20:43 GMT
> 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);
}
}
yingjian.ma1955@gmail.com - 26 Apr 2006 23:28 GMT
It works.  Thank you very much.

Could you tell me why my JApplet did not work?
Amfur Kilnem - 26 Apr 2006 23:43 GMT
> 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.
Oliver Wong - 27 Apr 2006 19:00 GMT
> 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/tutorial/deployment/applet/param.html

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.