Results 1 to 3 of 3
  1. #1
    Kratos321's Avatar
    Kratos321 is offline Member
    Join Date
    May 2011
    Posts
    31
    Rep Power
    0

    Default NullPointerException with applets?

    The following code is supposed to create an applet to convert from degrees to farenheit or vice versa.


    // The "Ex11TempConvertApplet" class.
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;

    public class Ex11TempConvertApplet extends Applet
    {
    double inputTemp;
    Label prompt,output;
    TextField input;
    Checkbox cToF, fToC;

    public void init ()
    {
    setSize (150, 200);

    prompt = (new Label ("Enter the temperature:"));
    input = (new TextField (10));

    CheckboxGroup cbg = new CheckboxGroup ();
    cToF = (new Checkbox ("\u00B0C to \u00B0F", cbg, false));
    // cToF.addActionListener(this);
    fToC = (new Checkbox ("\u00B0F to \u00B0C", cbg, false));

    setLayout (new FlowLayout (FlowLayout.CENTER));
    Button b = (new Button ("Convert"));

    add (prompt);
    add (input);
    add (cToF);
    add (fToC);
    add (b);


    } // init method




    public boolean action (Event e, Object o)
    {
    inputTemp = Double.parseDouble (input.getText ());
    if (e.target instanceof Checkbox)
    {
    if (cToF.getState () == true)
    {
    output.setText (Double.toString (tempConvert (inputTemp, 0)));
    } //end of inner if
    else if (fToC.getState () == true)
    {
    output.setText (Double.toString (tempConvert (inputTemp, 1)));

    } //end of inner else if
    return true;
    }
    //end of outer if
    return true;

    }



    public static double tempConvert (double temp, int convertType)
    {
    double convertedProduct;

    if (convertType == 1)
    {
    convertedProduct = (5 * (temp - 32)) / 9;
    convertedProduct = (Math.round ((convertedProduct) * 100));
    convertedProduct = convertedProduct / 100;
    }
    else
    {
    convertedProduct = (((9 * temp) / 5) + 32);
    convertedProduct = (Math.round ((convertedProduct) * 100));
    convertedProduct = convertedProduct / 100;
    }

    return convertedProduct;

    } //end of sub-method


    public void paint (Graphics g)
    {
    // Place the body of the drawing method here
    } // paint method
    } // Ex11TempConvertApplet class


    However, when I run the program, type a number and click on a checkbox, I get the following error:


    java.lang.NullPointerException
    at Ex11TempConvertApplet.action(Ex11TempConvertApplet .java:51)
    at java.awt.Component.handleEvent(Unknown Source)
    at java.awt.Component.postEvent(Unknown Source)
    at java.awt.Component.postEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierar chy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    Any help would be appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: NullPointerException with applets?

    This line at Ex11TempConvertApplet.action(Ex11TempConvertApplet .java:51) suggests that the problem is on line 51 of the Ex11TempConvertApplet class . So that begs the question -- which line is this?

  3. #3
    Kratos321's Avatar
    Kratos321 is offline Member
    Join Date
    May 2011
    Posts
    31
    Rep Power
    0

    Default Re: NullPointerException with applets?

    Nevermind, I figured it out, I actually forgot to create a text field for the output thanks anyways.

Similar Threads

  1. Applets
    By MBD in forum New To Java
    Replies: 2
    Last Post: 11-02-2011, 03:49 AM
  2. Applets
    By rdjava in forum Advanced Java
    Replies: 2
    Last Post: 01-27-2011, 12:07 AM
  3. I need help with Applets
    By ProgrammingPup in forum Advanced Java
    Replies: 1
    Last Post: 12-22-2009, 08:07 PM
  4. Can't see any Applets
    By pyr0chem in forum Java Applets
    Replies: 9
    Last Post: 12-17-2008, 04:24 AM
  5. applets on mac what´s going on?
    By willemjav in forum Java Applets
    Replies: 6
    Last Post: 04-19-2008, 07:03 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •