0

I am having an issue handling button events. I am creating a program that lets the user select the type of pizza they want and the program calculates the price of the pizza. I have my layout setup, however when a medium pizza is selected the button is not processing the proper response. Can anyone give me some advice? I have looked over my code for the past hour and I just can't seem to see the error I am making. Here is my code...

public class Prog9Frame extends JFrame implements ActionListener
{
private JLabel title;
private JLabel size;
private JLabel toppings;
private JComboBox crust;
private JRadioButton mediumRadio;
private JRadioButton largeRadio;
private JRadioButton xLargeRadio;
private JCheckBox pepperoniBox;
private JCheckBox sausageBox;
private JCheckBox mushroomsBox;
private JCheckBox onionsBox;
private JLabel total;
private JTextField totalField;
private JButton submit;

public Prog9Frame()
{
    super ("Pizzazz Pizza");
    setLayout( new BorderLayout( 5,5 ) );

    //north region
    title = new JLabel ( "Pizzazz Pizza", JLabel.CENTER );
    add ( title, BorderLayout.NORTH);

    //west region
    JPanel westPanel = new JPanel();
    westPanel.setLayout( new BoxLayout( westPanel, BoxLayout.Y_AXIS ) );

    westPanel.add(Box.createRigidArea( new Dimension( 25,1 )) );
    size = new JLabel ( "Size" );
    westPanel.add( Box.createVerticalStrut((20)) );
    westPanel.add( size );
    mediumRadio = new JRadioButton( "Medium" );
    westPanel.add(Box.createVerticalStrut(20) );
    westPanel.add( mediumRadio );
    largeRadio = new JRadioButton( "Large ");
    westPanel.add(Box.createVerticalStrut(20));
    westPanel.add(largeRadio);
    xLargeRadio = new JRadioButton( "X-Large ");
    westPanel.add(Box.createVerticalStrut(20));
    westPanel.add(xLargeRadio);
    add(westPanel, BorderLayout.WEST);

    //center region
    JPanel centerPanel = new JPanel();
    centerPanel.setLayout( new BoxLayout(centerPanel, BoxLayout.Y_AXIS ));

    toppings = new JLabel ( "Toppings" );
    centerPanel.add(Box.createVerticalStrut(( 20 )) );
    centerPanel.add( toppings );
    pepperoniBox = new JCheckBox( "Pepperoni" );
    centerPanel.add(Box.createVerticalStrut(( 20 )) );
    centerPanel.add( pepperoniBox);
    sausageBox = new JCheckBox( "Sausage" );
    centerPanel.add(Box.createVerticalStrut(( 20 )) );
    centerPanel.add( sausageBox);
    mushroomsBox = new JCheckBox( "Mushrooms" );
    centerPanel.add(Box.createVerticalStrut(( 20 )) );
    centerPanel.add( mushroomsBox);
    onionsBox = new JCheckBox( "Onions" );
    centerPanel.add(Box.createVerticalStrut(( 20 )) );
    centerPanel.add( onionsBox);
    add( centerPanel, BorderLayout.CENTER);

    //east region
    JPanel eastPanel = new JPanel();
    eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.Y_AXIS));
    eastPanel.add(Box.createHorizontalStrut(20));
    eastPanel.add(Box.createVerticalStrut(50));
    String[] crustStrings = { "Thin", "Regular", "Deep Dish" };
    JComboBox crust = new JComboBox(crustStrings);
    eastPanel.add(crust);
    eastPanel.add(Box.createVerticalStrut(200));
    add( eastPanel, BorderLayout.EAST);

    //south region
    JPanel southPanel = new JPanel();
    southPanel.setLayout(new FlowLayout( FlowLayout.CENTER) );

    JTextField totalField = new JTextField(10);
    southPanel.add(totalField);
    JButton submit = new JButton ("Submit");
    submit.addActionListener( this );
    southPanel.add( submit );
    add( southPanel, BorderLayout.SOUTH);


}
//handle button events
public void actionPerformed( ActionEvent event )
{
if (mediumRadio.isSelected())
    {
        double pizzaMed = 7.95;
        totalField.setText(new DecimalFormat("###00.00").format(pizzaMed));
        }
    }

}
Mike
  • 19
  • 1
  • 4

2 Answers2

3

You're shadowing the totalField variable by re-declaring it in the class's constructor. This will cause you the re-declared variable, the one that has a valid object reference to be visible only in the constructor, only within the scope within which it was declared, and it will leave the class field unconstructed and thus null. If you try to use it, you'll get a NullPointerException or NPE.

The solution is not to re-declare the variable in the constructor but rather to initialize the class field there (or on declaration in the class if need be).

So rather than:

public class Foo {
   private JTextField bar;

   public Foo() {
     JTextField bar = new JTextField(10);  // re-declaring bar here
   }
}

do:

public class Foo {
   private JTextField bar;

   public Foo() {
     bar = new JTextField(10);  // ** see the difference? **
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Can you also mention that the OP hasn't actually registered an `ActionListener` against anything other then the `submit` button :P – MadProgrammer May 10 '13 at 01:07
  • 1
    @MadProgrammer: that's probably all he really should add an ActionListener to. – Hovercraft Full Of Eels May 10 '13 at 01:08
  • To @NicRobertson (who deleted his answer): I politely disagree. The JRadioButtons should be added to a ButtonGroup, but should not have an ActionListener added. Only the button that is pressed when the user is done creating their pizza, the submit JButton, should have an ActionListener added to it. – Hovercraft Full Of Eels May 10 '13 at 01:12
  • 1
    Yeah I realize that, my mistake. I didn't see the action listener for the submit button until it was pointed out in the comment above. – Nic Robertson May 10 '13 at 01:14
  • @MadProgrammer: wow, it looks like my answer was not *concrete* enough for the OP. Not even a comment. To be believed. – Hovercraft Full Of Eels May 10 '13 at 01:38
  • @HovercraftFullOfEels I'm trying to figure out what Ahmars answer actually added that yours didn't cover or say differently some 20 minutes earlier...seems to be a lot of "shadow" answers appearing lately ... – MadProgrammer May 10 '13 at 01:48
  • @MadProgrammer: His gave concrete code that solved the problem rather than a generalization that just solved the logic behind the problem. – Hovercraft Full Of Eels May 10 '13 at 01:51
  • @HovercraftFullOfEels God forbid we actually make posters actually have to think :P – MadProgrammer May 10 '13 at 01:52
1

Update your code for south region. You are re-declaring totalField which hide in actionPerformed method.That's way it gives java.lang.NullPointerException.

JTextField totalField = new JTextField(10);

change to

this.totalField = new JTextField(10);
Ahmar
  • 3,717
  • 2
  • 24
  • 42
  • Thanks so much! I knew I had to be missing something simple. I really appreciate the help – Mike May 10 '13 at 01:29