NullPointerException

What creates a NullPointerException? I have a program converting gallons to quarts , pint, gills, liters, and fluid. Whenever i run the code it gives me the NullPointer Exception.
[186 byte] By [wash_ema] at [2007-10-1 0:45:43]
# 1

In general?

1) Trying to operate on a variable that is null:

String s = null;

s.length(); // <-- throws NullPointerException

2) Trying to pass null to a method which specifically checks for null arguments and throws said exception.

String s = null;

doSomething(s); // <-- throws NullPointerException

public void doSomething(String str) {

if(str == null) {

throw new NullPointerException("Can't do something with nothing.");

}

}

Specifically with regards to your code, you are hitting one or the other of those situations...

bsampieria at 2007-7-8 0:59:17 > top of Java-index,Security,Event Handling...
# 2

Well this is my code. Are you referring to when i am converting from a string to a double in the action performed method?

import java.awt.*;

import java.io.*;

import java.awt.event.*;

import java.util.*;

public class Quiz08 extends FrameWithClose implements ActionListener {

LabeltestLabel;

TextField myTextBox;

//TextField txtinfo;

TextField txtAnswer;

ButtongetTextButton;

ButtonG2QtButton;

Button G2PtButton;

Button G2LButton;

ButtonG2GilButton;

ButtonG2FozButton;

ButtonExitButton;

double str;

double gallons = 1;

double liters = 3.785;

int gallon = 1;

int quart = 4;

int pint = 8;

int gill = 32;

int fluid = 128;

public static void main(String args [])

{

Quiz08 tf = new Quiz08();

tf.setSize(400,400);

tf.setVisible(true);

}

public Quiz08 (){

setLayout(new FlowLayout());

setBackground(Color.blue);

testLabel = new Label(" a simple test of a TEXT box ");

add(testLabel);

getTextButton= new Button ("get the text");

add(getTextButton);

getTextButton.addActionListener(this);

myTextBox = new TextField();

myTextBox.setText("12 24 15 76 54 15");

txtAnswer = new TextField();

txtAnswer.setText("");

Panel p1 = new Panel();

p1.setLayout(new FlowLayout());

add("Center",p1);

Panel p2 = new Panel();//G2GQt, G2Pt,G2Gil,G2Foz buttons

p2.setLayout(new GridLayout(2,0));

p2.add(G2QtButton);

p2.add(G2PtButton);

p2.add(G2GilButton);

p2.add(G2FozButton);

add("West", p2);

G2QtButton.addActionListener(this);

G2PtButton.addActionListener(this);

G2GilButton.addActionListener(this);

G2FozButton.addActionListener(this);

Panel p3 = new Panel();//G2L and Exit Button

p3.setLayout(new GridLayout(2,0));

p3.add(G2LButton);

p3.add(ExitButton);

add("East", p3);

G2LButton.addActionListener(this);

ExitButton.addActionListener(this);

Panel p4 = new Panel();

p4.setLayout(new FlowLayout());

p4.add(txtAnswer);

add("South",p4);

Panel p5 = new Panel();

p5.setLayout(new FlowLayout());

p5.add(myTextBox);

add("North", p5);

} // end constructor

public void actionPerformed(ActionEvent evt) {

try {

String t = myTextBox.getText();

Double ts = new Double(t);

str = ts.doubleValue();

}

catch (NumberFormatException e) {

txtAnswer.setText("invalid number entered.");

return; // Break out of the actionPerformed method.

}

String s = evt.getActionCommand();

if (evt.getSource() instanceof Button) {

if (s.equals("ExitButton")){

dispose();

System.exit(0);

}

if (s.equals("G2Qt")){

quart = quart%gallon;

txtAnswer.getText();

}

if (s.equals("G2Pt")){

pint = pint%gallon;

txtAnswer.getText();

//ts = pint;

//txtAnswer.setText(pint);

//String t = G2PtButton;

}

if (s.equals("G2Gil")){

gill = gill%gallon;

txtAnswer.getText();

//ts = pint;

//txtAnswer.setText(pint);

//String t = G2GilButton;

}

if (s.equals("G2Foz")){

fluid = fluid%gallon;

txtAnswer.getText();

//ts = fluid;

//txtAnswer.setText(pint);

//String t = G2FozButton;

}

if (s.equals("G2L")){

liters = gallons%liters;

txtAnswer.getText();

// ts = liters;

//String t = G2LButton;

}

} // end instanceof Button

//txtAnswer = ts;

} // end actionEvent

} // end class

wash_ema at 2007-7-8 0:59:17 > top of Java-index,Security,Event Handling...
# 3

next time, use the proper code tags, please...

In your action performed method, you should probably check for exit first... not that that matters in this problem, but just to avoid doing things if all you are going to do is quit.

It is not impossible that

String s = evt.getActionCommand();

is setting s to null... If the button doesn't have an action command, that's what it'll be, in which case, that would certainly cause you problems. But you can check for null...

if(s == null) {

return;

}

But otherwise, make sure you are setting action commands in the buttons (cuz I don't see that in the code you posted). Actually, that code doesn't actually create the G2QtButton and others, so maybe the NPE is coming from p2.add(G2QtButton);?

bsampieria at 2007-7-8 0:59:17 > top of Java-index,Security,Event Handling...
# 4

> Well this is my code. Are you referring to when i am

> converting from a string to a double in the action

> performed method?

If you look at the error message, it should tell you in which method and in which line the null pointer exception occurs.

It looks to me like the G2xxx Buttons are never set using "new Button()"

atmguya at 2007-7-8 0:59:17 > top of Java-index,Security,Event Handling...