Java GUI空白和nullpointerexception

Java GUI空白和nullpointerexception,java,swing,methods,nullpointerexception,Java,Swing,Methods,Nullpointerexception,我修正了现在当我点击我的计算按钮时,我会得到以下结果: 这是所有工作,除了计算按钮,现在,这是最重要的部分。提前感谢大家的帮助 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Radio$CalcButtonListener.actionPerformed(Radio.java:76) at javax.swing.AbstractButton.fireActionPerformed

我修正了现在当我点击我的计算按钮时,我会得到以下结果:

这是所有工作,除了计算按钮,现在,这是最重要的部分。提前感谢大家的帮助

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Radio$CalcButtonListener.actionPerformed(Radio.java:76)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)






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 final int WINDOW_WIDTH = 550;
        private final int WINDOW_HEIGHT = 550;
        double pTotal;
        double sTotal;

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

        }


        private void buildPanel() 
        {    
            Panel = new JPanel();
            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);
            Panel.add(squareArea);
            Panel.add(parallelogramArea);
            Panel.add(calcButton);
        }




        public static void main (String[] args)
        {

            Radio radio = new Radio();
            radio.buildPanel();
        }

        private class CalcButtonListener implements ActionListener
        {

            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);
                }
            }
        }
    }

我认为您的
按钮面板
为空。但是您可以逐行调试代码,找出其中哪一个是空的

您不需要在任何地方实例化
面板
。在
buildPanel
中,您可以添加:

panel = new JPanel();

另外,
按钮面板
未初始化:

buttonPanel = new JPanel();
然后需要将面板添加到您的
JFrame

add(panel);

注意,Java命名约定显示变量名以小写字母开头,这将使
Panel
Panel
您没有创建Panel的对象

JPanel panel = new JPanel();

您需要实例化Panel和buttonPanel。您可以在构造函数中执行此操作

    public Radio(){

        Panel = new JPanel();
        buttonPanel = new JPanel();
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setTitle("Area Calculator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        buildPanel();
        add(Panel);
        setVisible(true);

    }

顺便说一句,在将可见性设置为true之前,调用buildPanel()并添加面板。

JPanel panelName=new JPanel()
:)比答案更重要的是学习如何调试NPE。您需要检查行中的变量,找出哪一个是空的,然后追溯到代码中,看看为什么它没有初始化。