Java Applet question

Y

yingjian.ma1955

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.
 
A

Amfur Kilnem

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?

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...?
 
Y

yingjian.ma1955

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?
 
Y

yingjian.ma1955

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.
 
D

Dag Sunde

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?
 
Y

yingjian.ma1955

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.
 
A

Amfur Kilnem

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);
}
}
 
Y

yingjian.ma1955

It works. Thank you very much.

Could you tell me why my JApplet did not work?
 
A

Amfur Kilnem

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top