Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

actionPerformed ... Passing form values to a method

Status
Not open for further replies.

asiavoices

Programmer
Sep 20, 2001
90
CA
Hello all,

I need help in figuring out an error during execution of my program.

How I got the error...

1. Using the GUI I created, I entered the info on the
form.

2. I clicked on an "ADD" button to add items to an Array.

3. That's when I got the error when I try to display the values.


Exception occurred during event dispatching:
java.lang.NullPointerException
at MyProg.actionPerformed(Assig10.java:374)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1450)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1504)
......


Here is my code.........


/* ================================
* = Create the JTextFields =
* ================================ */

JTextField nameField = new JTextField(15);
JTextField moneyField = new JTextField(8);
JCheckBox isTaxField = new JCheckBox("Yes", true);
isTaxField.setMnemonic(KeyEvent.VK_Y);
isTaxField.setForeground(new Color(0,0,0));
isTaxField.setFont(new Font("Verdana", Font.BOLD, 11));




/* ========================================================
* = actionPerformed() method from =
* = ActionListener interface =
* ======================================================== */

public void actionPerformed(ActionEvent theButtonEvent)
{

if ( theButtonEvent.getActionCommand().equalsIgnoreCase("ADD"))
{
recordCounter ++;

String theName = nameField.getText();
double thePrice = Double.parseDouble(moneyField.getText().trim());
boolean isTaxable = isTaxField.isSelected();

/* ---- Add the item method disabled for now ------*/
// addItem(theName, thePrice, isTaxable);

/*------------ for debugging... what are the values? ----------*/

System.out.println(theName + " " + thePrice + " " + isTaxable);

}
}



Desired action:

1. Print out the values of the 2 fields and checkbox.

2. If successful, they will be used as arguments to a method that will insert this new record in an ArrayList.


Thanks for your help,

Christopher

 
What line of code is 374? Most likely a null String that you are trying to call a method on. If there is any possible ways for your variables to be null then you need to check for this condition before calling their methods.
 
Hi Wushutwist,

The code on line 374 is that from the first statement:

String theName = nameField.getText();

There were no null values because I entered something there. It just seems that it does not want to accept the above 3 statements, starting with the first field (nameField JtextField):

String theName = nameField.getText();
double thePrice = Double.parseDouble(moneyField.getText().trim());
boolean isTaxable = isTaxField.isSelected();

This is so frustrating :-(

After I get past this, I'd like to display the array in another JPanel (how do i specify where I'd like to put array in a specific JPanel? should I use JList to list them?)

Christopher

 
Still working on this one... its shouldn't be that weird shoud it?

arrrgh... ;-)

Christopher
 
Still working on this one... its shouldn't be that weird should it?

arrrgh... ;-)

Christopher
 
Still working on this one... its shouldn't be that weird should it?

arrrgh... ;-)

Christopher
 

Hi everyone that may be able to help me ;-)

I basically have tried various options. The "problem" before was what I thought was passing the boolean value. I commented them out and just wanted to see if I could print them out. If I could then I would assume, that at this point, I could pass them to my method.

Maybe a bit more code would help for clarification & debugging.

So if I enter my name and price and hit the add button, that's when I got the error. The Entire Error message is at the bottom of this post.


/* =============================
= My Code here.... =
============================= */


public class Christopher extends JFrame
{
.....
private String theName = "";
private double thePrice = 0.00d;
private boolean isTaxable = true;
.....

/* ---- declare JTextFields ------- */

private JTextField descriptionField = null;
private JTextField priceField = null;
private JCheckBox isTaxField = null;
......

/* ---- declare JButtons ------- */

private JButton addButton;
......


/* ---- setTotal() method ------- */

public void setTotal(double total)
{
this.total = total;
}

/* ---- getTotal() returned method ------- */

public double getTotal()
{
return total;
}

..............




/* ============ main () method =========== */


public static void main(String[] args)
{

Christopher myLayout = new Christopher();
myLayout.setVisible(true);

} // End of void main() method

.......



/* ============ My Constructor =========== */


public Christopher()
{
........

JTextField descriptionField = new JTextField(15);
descriptionField.addKeyListener(new CheckNameListener());

JTextField priceField = new JTextField(8);
priceField.addKeyListener(new CheckPriceListener());

JCheckBox isTaxField = new JCheckBox("Yes", true);
isTaxField.setMnemonic(KeyEvent.VK_Y);
isTaxField.setBackground(new Color(204,255,204));
isTaxField.setForeground(new Color(0,0,0));
isTaxField.setFont(new Font("Verdana", Font.BOLD, 11));

JButton addButton = new JButton("ADD ITEM");
addButton.addActionListener(new AddItemListener());
addButton.setMnemonic(KeyEvent.VK_A);
.........

fieldPanel.add(descriptionField);
fieldPanel.add(priceField);
fieldPanel.add(isTaxField);

.........


/* ======= AddItemListener Event Handling (the Error area) ======= */


class AddItemListener implements ActionListener
{
public void actionPerformed(ActionEvent addItemEvent)
{
line 759 ----> String theName = (new String(descriptionField.getText().trim()));
double thePrice = (new Double(priceField.getText())).doubleValue();
boolean isTaxable = isTaxField.isSelected();

System.out.println( "Name: " + theName + "\nPrice: " + thePrice + "\nTaxable: " + taxable );


// Note: if it's sucessful, I'd like to pass them to my method

addItem(theName, thePrice, isTaxable);

}
} end of class AddItemListener class

........



/* ======= Checks number of keystrokes in the Description field === */

class CheckNameListener extends KeyAdapter
{
public void keyPressed(KeyEvent theKeyEntered)
{
if (theKeyEntered.getKeyChar() == theKeyEntered.VK_BACK_SPACE && nameCharsCounter >= 1)
{
nameCharsCounter--;
System.out.println( "You have typed in: " + nameCharsCounter + " characters.");
}
else if (theKeyEntered.getKeyChar() != theKeyEntered.VK_BACK_SPACE)
{
nameCharsCounter++;
System.out.println( "You have typed in: " + nameCharsCounter + " characters.");
}
else
{
nameCharsCounter++;
System.out.println( "You have typed in: " + nameCharsCounter + " characters.");
}
} //end of keyPressed Method()

} // end of CheckNameListener class


.........


/* ======= addItem() method ======== */


public void addItem(String name, double thePrice, boolean taxState)
{
ArrayList myStuff = new ArrayList();

Item cidx = new Item(name, thePrice, taxState);
myStuff.add(cidx);
numrecords++;

showList(myStuff);

}




/* ======= showList() method ======== */

public void showList(ArrayList myStuff2)
{
Iterator ite = myStuff2.iterator();

while (ite.hasNext())
{
Item data = (Item)ite.next();
setTotal(getTotal() + data.getPrice());
System.out.print( "Name: " + data.getName()+ "\t\tPrice: " + data.getPrice() + "\t\tTaxable? " + data.getTax() + "\n");
} // End of While loop

/* ---- Print out the results ------- */

System.out.print( "Total Records: " + numrecords + "\n");
System.out.println( "Total Cost: " + total);

} // end of showList() method



} // End of Christopher() constructor





/* =============================
= ERROR MESSAGE.... =
============================= */

Exception occurred during event dispatching:
java.lang.NullPointerException
at Christopher$AddItemListener.actionPerformed(Assig10.java:759)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1450)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1504)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:378)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:216)
at java.awt.Component.processMouseEvent(Component.java:3715)
at java.awt.Component.processEvent(Component.java:3544)
at java.awt.Container.processEvent(Container.java:1164)
at java.awt.Component.dispatchEventImpl(Component.java:2593)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2451)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2216)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2125)
at java.awt.Container.dispatchEventImpl(Container.java:1200)
at java.awt.Window.dispatchEventImpl(Window.java:926)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)



So that's it. I'd like to keep it simple for now and just make sure it can display the values in the text fields then I'll pass them to the method to add the data.

Thanks all,

Christopher






 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top