Java Applet question

Discussion in 'Java' started by yingjian.ma1955@gmail.com, Apr 25, 2006.

  1. Guest

    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.
    , Apr 25, 2006
    #1
    1. Advertising

  2. Amfur Kilnem Guest

    <> wrote in message
    news:...
    >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...?
    Amfur Kilnem, Apr 25, 2006
    #2
    1. Advertising

  3. Guest

    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?
    , Apr 25, 2006
    #3
  4. Guest

    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.
    , Apr 25, 2006
    #4
  5. Dag Sunde Guest

    <> skrev i melding
    news:...
    > 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.
    Dag Sunde, Apr 25, 2006
    #5
  6. Guest

    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.
    , Apr 26, 2006
    #6
  7. Amfur Kilnem Guest

    <> wrote in message
    news:...
    > 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);
    }
    }
    Amfur Kilnem, Apr 26, 2006
    #7
  8. Guest

    It works. Thank you very much.

    Could you tell me why my JApplet did not work?
    , Apr 26, 2006
    #8
  9. Amfur Kilnem Guest

    <> wrote in message
    news:...
    > 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.
    Amfur Kilnem, Apr 26, 2006
    #9
  10. Oliver Wong Guest

    <> wrote in message
    news:...
    > 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
    Oliver Wong, Apr 27, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. webster
    Replies:
    0
    Views:
    847
    webster
    Jul 20, 2003
  2. Krista
    Replies:
    3
    Views:
    2,310
    Andrew Thompson
    Sep 15, 2004
  3. Archana
    Replies:
    1
    Views:
    1,379
    Andrew Thompson
    Oct 25, 2004
  4. yawnmoth
    Replies:
    1
    Views:
    886
    Andrew Thompson
    Aug 15, 2006
  5. Replies:
    4
    Views:
    5,848
    Laurent D.A.M. MENTEN
    Jan 11, 2007
Loading...

Share This Page