3 Replies - 1474 Views - Last Post: 02 March 2011 - 02:32 PM Rate Topic: -----

#1 byancoskie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-March 11

Exception in thread "main" java.lang.NullPointerException I ne

Posted 02 March 2011 - 01:12 PM

I'M TRYING TO RUN THE FOLLOWING JAVA APPLICATION AND I RECEIVE THIS ERROR:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1031)
at java.awt.Container.add(Container.java:352)
at PaymentPanel.<init>(PaymentApp.java:75)
at PaymentFrame.<init>(PaymentApp.java:22)
at PaymentApp.main(PaymentApp.java:10)

My Code:
  import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class PaymentApp
{
    public static void main(String[] args)
    {
        JFrame frame = new PaymentFrame();
        frame.setVisible(true);
    }
}

class PaymentFrame extends JFrame
{
    public PaymentFrame()
    {
        setTitle("Payment Application");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new PaymentPanel();
        this.add(panel);
        this.pack();
        centerWindow(this);
    }

    private void centerWindow(Window w)
    {
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();
        setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
    }
}

class PaymentPanel extends JPanel implements ActionListener
{
	private JRadioButton creditCardRadioButton,
	                     billCustomerRadioButton;

	private JTextField   cardNumberTextField;

    private JLabel       cardTypeLabel,
                         cardNumberLabel,
                         expirationDateLabel;

    private JButton      acceptButton,
                         exitButton;

    private JComboBox    year,
                         month;

    private JCheckBox    verifiedCheckBox;

    private JList        cardTypeList;


    public PaymentPanel()
    {
		setLayout(new GridBagLayout());

		Border loweredBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED);

        // radio button panel
        JPanel radioPanel = new JPanel();
        ButtonGroup billingGroup = new ButtonGroup();
        radioPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        radioPanel.setBorder(
			BorderFactory.createTitledBorder(loweredBorder, "Billing:"));

        // credit card radio button
        creditCardRadioButton = new JRadioButton("Credit card", true);
        creditCardRadioButton.addActionListener(this);
        billingGroup.add(creditCardRadioButton);
        radioPanel.add(billCustomerRadioButton);

        // bill customer radio button
        billCustomerRadioButton = new JRadioButton("Bill customer");
        billCustomerRadioButton.addActionListener(this);
        billingGroup.add(billCustomerRadioButton);
        radioPanel.add(billCustomerRadioButton);

        add(radioPanel, getConstraints(0,0,3,1, GridBagConstraints.WEST));

        // card type label
        cardTypeLabel = new JLabel("Card type:");
        add(cardTypeLabel, getConstraints(0,1,1,1, GridBagConstraints.EAST));

        // card type list
        String[] cardNames
             = {"Visa", "Master Card", "American Express", "Other"};
        JList cardTypeList = new JList(cardNames);
        cardTypeList.setFixedCellWidth(170);
        cardTypeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        cardTypeList.setVisibleRowCount(3);
        JScrollPane cardTypeScrollPane = new JScrollPane(cardTypeList);
        add(cardTypeScrollPane,
             getConstraints(1,1,2,1, GridBagConstraints.WEST));

        // card number label
        cardNumberLabel = new JLabel("Card number:");
        add(cardNumberLabel,
             getConstraints(0,2,1,1, GridBagConstraints.EAST));

        // card number text field
        cardNumberTextField = new JTextField(15);
        add(cardNumberTextField, getConstraints(1,2,2,1, GridBagConstraints.WEST));

        // expiration date label
        expirationDateLabel = new JLabel("Expiration date:");
        add(expirationDateLabel, getConstraints(0,3,1,1, GridBagConstraints.EAST));

        // month combo box
        String[] months = { "January", "February", "March", "April", "May", "June",
                            "July", "August", "September", "October", "November",
                            "December" };

        JComboBox month = new JComboBox(months);
        add(month, getConstraints(1,3,1,1, GridBagConstraints.WEST));

        // year Combo Box
        String[] years = { "2005", "2006", "2007", "2008", "2009", "2010" };
        JComboBox year = new JComboBox();
        add(year, getConstraints(2,3,1,1, GridBagConstraints.WEST));

        // verified check box
        verifiedCheckBox = new JCheckBox("Verified");
        add(verifiedCheckBox,
              getConstraints(1,4,1,1, GridBagConstraints.WEST));

        // accept button
        acceptButton = new JButton("Accept");
        acceptButton.addActionListener(this);
        add(acceptButton, getConstraints(1,5,1,1, GridBagConstraints.EAST));

        // exit button
        exitButton = new JButton("Exit");
        exitButton.addActionListener(this);
        add(exitButton, getConstraints(2,5,1,1, GridBagConstraints.CENTER));

    }

    //Method for setting gridbag constraints
	private GridBagConstraints getConstraints(int gridx, int gridy,
	int gridwidth, int gridheight, int anchor)
	{
			GridBagConstraints c = new GridBagConstraints();
			c.insets = new Insets(5, 5, 5, 5);
			c.ipadx = 0;
			c.ipady = 0;
			c.gridx = gridx;
			c.gridy = gridy;
			c.gridwidth = gridwidth;
			c.gridheight = gridheight;
			c.anchor = anchor;
			return c;
    }

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source == exitButton)
            System.exit(0);
        else if (source == acceptButton)
        {
			String msg = "";
			if (creditCardRadioButton.isSelected())
			{
				msg = "Bill " + (String)cardTypeList.getSelectedValue() +
				    "\nNumber + " + cardNumberTextField.getText() +
				    "\nExpiration date: " +
				    (String)month.getSelectedItem() +
				    ", " + (String)year.getSelectedItem();
				    if (verifiedCheckBox.isSelected())
				       msg+= "\nCard has been verified,";
				    else
				       msg+= "\nCard has not been verified.";
		    }
		    else
		        msg = "Customer will be billed.";
		    JOptionPane.showMessageDialog(this, msg);
		    cardTypeList.setSelectedIndex(0);
		    cardNumberTextField.setText("");
		    month.setSelectedIndex(0);
		    year.setSelectedIndex(0);
		    verifiedCheckBox.setSelected(false);
	     }
	     else if (source == creditCardRadioButton
	          ||  source == billCustomerRadioButton)
	     {
			  if (creditCardRadioButton.isSelected())
			      enableCreditCardControls(true);
			  else if (billCustomerRadioButton.isSelected())
			      enableCreditCardControls(false);
	     }
	}

	private void enableCreditCardControls(boolean enable)
	{
		cardTypeLabel.setEnabled(enable);
		cardTypeList.setEnabled(enable);
		cardNumberLabel.setEnabled(enable);
		cardNumberTextField.setEnabled(enable);
		expirationDateLabel.setEnabled(enable);
		month.setEnabled(enable);
		year.setEnabled(enable);
		verifiedCheckBox.setEnabled(enable);

    }
}


Mod edit - Please :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.lang.NullPointerException I ne

#2 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Exception in thread "main" java.lang.NullPointerException I ne

Posted 02 March 2011 - 01:36 PM

If you look at this bit of code below:
    radioPanel.add(billCustomerRadioButton);

        // bill customer radio button
        billCustomerRadioButton = new JRadioButton("Bill customer");
        billCustomerRadioButton.addActionListener(this);
        billingGroup.add(billCustomerRadioButton);
        radioPanel.add(billCustomerRadioButton);


Your adding billCustomerRadioButton before you instantiate the JButton thus throwing a NullPointerException since it hasn't been initialised yet. Also, seems like you add it to the panel after you instantiate the JButton so get wrid of line 75.

This post has been edited by m-e-g-a-z: 02 March 2011 - 01:38 PM

Was This Post Helpful? 1
  • +
  • -

#3 byancoskie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-March 11

Re: Exception in thread "main" java.lang.NullPointerException I ne

Posted 02 March 2011 - 01:52 PM

View Postm-e-g-a-z, on 02 March 2011 - 01:36 PM, said:

If you look at this bit of code below:
    radioPanel.add(billCustomerRadioButton);

        // bill customer radio button
        billCustomerRadioButton = new JRadioButton("Bill customer");
        billCustomerRadioButton.addActionListener(this);
        billingGroup.add(billCustomerRadioButton);
        radioPanel.add(billCustomerRadioButton);


Your adding billCustomerRadioButton before you instantiate the JButton thus throwing a NullPointerException since it hasn't been initialised yet. Also, seems like you add it to the panel after you instantiate the JButton so get wrid of line 75.

Was This Post Helpful? 0
  • +
  • -

#4 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Exception in thread "main" java.lang.NullPointerException I ne

Posted 02 March 2011 - 02:32 PM

View Postbyancoskie, on 02 March 2011 - 08:52 PM, said:

View Postm-e-g-a-z, on 02 March 2011 - 01:36 PM, said:

If you look at this bit of code below:
    radioPanel.add(billCustomerRadioButton);

        // bill customer radio button
        billCustomerRadioButton = new JRadioButton("Bill customer");
        billCustomerRadioButton.addActionListener(this);
        billingGroup.add(billCustomerRadioButton);
        radioPanel.add(billCustomerRadioButton);


Your adding billCustomerRadioButton before you instantiate the JButton thus throwing a NullPointerException since it hasn't been initialised yet. Also, seems like you add it to the panel after you instantiate the JButton so get wrid of line 75.


Your question is? :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1