1

This is something that is happening in eclipse, I have a package and a have stored the applet file and the HTMLL file in the same package. (Is this the way to write an applet in eclipse?). The code is is supposed to take parameters from an HTML file to change the color, font etc on a message on a panel. The program also has a default message.

The default message gets printed when the program is run (if run w/o the HTML file) but when the parameters from the HTML file are used, I get a NPE.

Is there something I am doing wrong?

Here is the code and the corresponding HTML file for parameters.

import java.applet.Applet;
import java.awt.*;
import javax.swing.*;

public class Exercise18_04 extends JApplet {
  private String message; // Message to display
  private int x = 20; // Default x coordinate
  private int y = 20; // Default y coordinate
  private String color;
  private String fontName;
  private int fontSize = 20;

  // Initialize the applet
  public void init() {
    // Get parameter values from the HTML file
    message = getParameter("MESSAGE");
    x = Integer.parseInt(getParameter("X"));
    y = Integer.parseInt(getParameter("Y"));
    color = getParameter("COLOR");
    fontName = getParameter("FONTNAME");
    fontSize = Integer.parseInt(getParameter("FONTSIZE"));

    // Create a message panel
    MessagePanel messagePanel = new MessagePanel(message);
    messagePanel.setXCoordinate(x);
    messagePanel.setYCoordinate(y);

    if (color.equals("red")) {
      messagePanel.setForeground(Color.red);
    }
    else if (color.equals("yellow"))
      messagePanel.setForeground(Color.yellow);
    else if (color.equals("green"))
      messagePanel.setForeground(Color.green);

    messagePanel.setFont(new Font(fontName, Font.BOLD, fontSize));

    // Add the message panel to the applet
    add(messagePanel);
  }
}
<html>
<body>
<applet
  code = "Exercise18_04.class"
     width = 250
  height = 80 

>
  <param name = MESSAGE value = "Welcome to Java" />
  <param name = X value = 40 />
  <param name = Y value = 50 />
  <param name = COLOR value = "red" />
  <param name = FONTNAME value = "Monospaced" />
  <param name = FONTSIZE value = 20 />
</applet>

</body>
</html>
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Cielo S
  • 11
  • 1
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). .. – Andrew Thompson Dec 25 '14 at 23:43
  • .. 3) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 4) Always copy/paste error and exception output! – Andrew Thompson Dec 25 '14 at 23:43

0 Answers0