Skip to content
Advertisement

Java Radio Button Variable Errors

So this works now, and i fixed the variable calling errors. but I get :

Exception in thread "main" java.lang.NullPointerException
    at Radio.buildPanel(Radio.java:56)
    at Radio.<init>(Radio.java:33)
    at Radio.main(Radio.java:74)

My GUI Pops up but is blank, what is it now? I cannot figure out what the problem is at this time.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;




public class Radio extends JFrame 
{

    private JPanel Panel;
    private JPanel buttonPanel;
    private JTextField base;
    private JTextField width;
    private JRadioButton squareArea;
    private JRadioButton parallelogramArea;
    private JLabel messageLabel;
    private JTextField text;
    private final int WINDOW_WIDTH = 550;
    private final int WINDOW_HEIGHT = 550;
    private ButtonGroup radioButtonGroup;
    private JRadioButton radioButton1;
    private JRadioButton radioButton2;
    double pTotal;
    double sTotal;

    public Radio()
    {
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setTitle("Area Calculator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        buildPanel();
        add(Panel);

    }

    public void init()
    {
     setLayout(new BorderLayout());
     add(buttonPanel, BorderLayout.SOUTH);
    }
    private void buildPanel() 
    {
        JLabel messageLabel1 = new JLabel("Please enter the base: ");
        JTextField base = new JTextField(10);
        JLabel messageLabel2 = new JLabel("Please enter the width: ");
        JTextField width = new JTextField(10);
        JRadioButton squareArea = new JRadioButton("Choice 1", true);
        JRadioButton parallelogramArea = new JRadioButton("Choice 2");
        ButtonGroup group = new ButtonGroup();
        JButton calcButton = new JButton("Calculate");
        calcButton.setBackground(Color.BLUE);
        calcButton.setForeground(Color.PINK);
        calcButton.addActionListener(new CalcButtonListener());
        Panel.add(messageLabel1);
        Panel.add(base);
        Panel.add(messageLabel2);
        Panel.add(width);
        group.add(squareArea);
        group.add(parallelogramArea);
        buttonPanel.add(squareArea);
        buttonPanel.add(parallelogramArea);
        Panel.add(calcButton);
        Panel.add(buttonPanel);
    }




    public static void main (String[] args)
    {

        Radio radio = new Radio();
    }

    private class CalcButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e) 
        {


            if (parallelogramArea.isSelected());
            {
                pTotal = Double.parseDouble(base.getText()) * Double.parseDouble(width.getText());
                JOptionPane.showMessageDialog(null, "The Area is: " + pTotal);
            }


            if (squareArea.isSelected())
            {

                 sTotal = Double.parseDouble(base.getText()) * Double.parseDouble(width.getText());
                 JOptionPane.showMessageDialog(null, "The Area is: " + sTotal);
            }
        }
    }
}

This is very important to get resolved, thank you in advance.

Advertisement

Answer

the first error means when you call buildPanel(String, String) you need to use 2 string parameters to pass to the function. At the moment you are not passing the parameters. Second error means the variables you attempt to use are undefined, because of scope issues meaning they are not defined when you try to use them inside a method.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement