Results 1 to 3 of 3
- 04-21-2012, 09:56 PM #1
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.
-
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?
- 04-21-2012, 10:18 PM #3
Similar Threads
-
Applets
By MBD in forum New To JavaReplies: 2Last Post: 11-02-2011, 03:49 AM -
Applets
By rdjava in forum Advanced JavaReplies: 2Last Post: 01-27-2011, 12:07 AM -
I need help with Applets
By ProgrammingPup in forum Advanced JavaReplies: 1Last Post: 12-22-2009, 08:07 PM -
Can't see any Applets
By pyr0chem in forum Java AppletsReplies: 9Last Post: 12-17-2008, 04:24 AM -
applets on mac what´s going on?
By willemjav in forum Java AppletsReplies: 6Last Post: 04-19-2008, 07:03 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks