NullPointerException:无法调用 "JComboBox.getSelectedIndex()" 因为 "this.this[=11=].comboBox_1" 为空

NullPointerException: Cannot invoke "JComboBox.getSelectedIndex()" because "this.this$0.comboBox_1" is null

我的组合框总是 return 的空值。在我的项目“考勤管理系统”中,我需要验证组合框中的所选项目,但它 return 为空。我怎样才能得到它 return 从组合框中选择的项目?

我使用 Eclipse IDE 来 运行 我的项目。

这是我的代码:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class admin extends javax.swing.JFrame {
    private static final long serialVersionUID=1L;
    @SuppressWarnings({ "unchecked", "rawtypes" })
    admin(){
    //setResizable(false);  
    JFrame newFrame=new JFrame("Admin");
    newFrame.getContentPane().setForeground(new Color(0, 0, 0));
    newFrame.getContentPane().setFont(new Font("Arial Black", Font.BOLD, 15));
    newFrame.getContentPane().setBackground(new Color(0, 191, 255));
    newFrame.setSize(1000,600);
    newFrame.setVisible(true);
    newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JLabel lblNewLabel = new JLabel("Welcome Admin");
    lblNewLabel.setBounds(416, 75, 185, 28);
    lblNewLabel.setFont(new Font("Arial Black", Font.BOLD, 18));
    newFrame.getContentPane().setLayout(null);
    newFrame.getContentPane().add(lblNewLabel);
    JButton btnNewButton = new JButton("Go");
    btnNewButton.setBorder(new BevelBorder(BevelBorder.RAISED, new Color(0, 0, 255), null, 
    Color.BLUE, null));
    btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 13));
    btnNewButton.setBounds(471, 343, 85, 35);
    btnNewButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        if(comboBox_1.getSelectedItem().toString().equals("Student"))
        {
    
       JOptionPane.showMessageDialog(null, "You can  Login !","", JOptionPane.ERROR_MESSAGE);
       //new Register_Student().setVisible(true); 
       }
    }
    });
    newFrame.getContentPane().add(btnNewButton);
    JComboBox comboBox_1 = new JComboBox();
    comboBox_1.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(0, 0, 255), null, new 
     Color(0, 0, 255), null));
     comboBox_1.setFont(new Font("Arial Black", Font.BOLD, 14));
     comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"Student", "Teacher", "Parent"}));

     comboBox_1.setBounds(359, 179, 291, 80);
     newFrame.getContentPane().add(comboBox_1);
      pack();
      }
    public static void main(String args[]) {
  
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new admin().setVisible(true);
            
        }
    });
    }
     private javax.swing.JComboBox<String> comboBox_1;
}

您已经创建了两个组合框对象。请移除顶部的 ComboBox 声明或构造函数中的 JComboBox comboBox_1 =comboBox_1 =

import java.awt.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
public class admin extends javax.swing.JFrame {
    private static final long serialVersionUID=1L;
    @SuppressWarnings({ "unchecked", "rawtypes" })
    admin(){
//setResizable(false);
        JFrame newFrame=new JFrame("Admin");
        newFrame.getContentPane().setForeground(new Color(0, 0, 0));
        newFrame.getContentPane().setFont(new Font("Arial Black", Font.BOLD, 15));
        newFrame.getContentPane().setBackground(new Color(0, 191, 255));
        newFrame.setSize(1000,600);
        newFrame.setVisible(true);
        newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel lblNewLabel = new JLabel("Welcome Admin");
        lblNewLabel.setBounds(416, 75, 185, 28);
        lblNewLabel.setFont(new Font("Arial Black", Font.BOLD, 18));
        newFrame.getContentPane().setLayout(null);
        newFrame.getContentPane().add(lblNewLabel);
        JComboBox comboBox_1 = new JComboBox();
        comboBox_1.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(0, 0, 255), null, new
                Color(0, 0, 255), null));
        comboBox_1.setFont(new Font("Arial Black", Font.BOLD, 14));
        comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"Student", "Teacher", "Parent"}));

        comboBox_1.setBounds(359, 179, 291, 80);
        JButton btnNewButton = new JButton("Go");
        btnNewButton.setBorder(new BevelBorder(BevelBorder.RAISED, new Color(0, 0, 255), null,
                Color.BLUE, null));
        btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 13));
        btnNewButton.setBounds(471, 343, 85, 35);
        btnNewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                if(comboBox_1.getSelectedItem().toString().equals("Student"))
                {

                    JOptionPane.showMessageDialog(null, "You can  Login !","", JOptionPane.ERROR_MESSAGE);
                    //new Register_Student().setVisible(true);
                }
            }
        });
        newFrame.getContentPane().add(btnNewButton);

        newFrame.getContentPane().add(comboBox_1);
        pack();
    }
    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new admin().setVisible(true);

            }
        });
    }
    private javax.swing.JComboBox<String> comboBox_1;
}

您正在隐藏变量。也就是说,您已声明 comboBox_1 两次,一次作为实例字段,一次作为局部变量...

public class admin extends javax.swing.JFrame {
    admin() {
        //...
        JComboBox comboBox_1 = new JComboBox();
        //...
    }
    //...
    private javax.swing.JComboBox<String> comboBox_1;
}

去掉本地decleration

public class admin extends javax.swing.JFrame {
    admin() {
        //...
        comboBox_1 = new JComboBox();
        //...
    }

话虽如此,您还有另一个问题。 adminJFrame 扩展...

public class admin extends javax.swing.JFrame {

但是你在构造函数中创建了第二个 JFrame...

public class admin extends javax.swing.JFrame {
    private static final long serialVersionUID = 1L;

    @SuppressWarnings({"unchecked", "rawtypes"})
    admin() {
        JFrame newFrame = new JFrame("Admin");

那么,当我执行 new admin().setVisble(true) 时,显示的是哪一帧?

作为一般建议,您应该避免直接从顶级容器扩展,例如 JFrame,您不会向 class 添加任何新功能,并且您将自己限制在一个用例。

而且,您真的非常非常想学习如何使用布局管理器,请参阅 Laying Out Components Within a Container 了解更多详细信息

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new Admin());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Admin extends JPanel {
        private static final long serialVersionUID = 1L;

        @SuppressWarnings({"unchecked", "rawtypes"})
        Admin() {
            setForeground(new Color(0, 0, 0));
            setBackground(new Color(0, 191, 255));

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.insets = new Insets(50, 0, 50, 0);

            JLabel lblNewLabel = new JLabel("Welcome Admin");
            lblNewLabel.setFont(new Font("Arial Black", Font.BOLD, 18));
            add(lblNewLabel, gbc);

            comboBox_1 = new JComboBox();
            comboBox_1.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(0, 0, 255), null, new Color(0, 0, 255), null));
            comboBox_1.setFont(new Font("Arial Black", Font.BOLD, 14));
            comboBox_1.setModel(new DefaultComboBoxModel(new String[]{"Student", "Teacher", "Parent"}));
            gbc.ipady = 100;
            gbc.insets = new Insets(0, 0, 0, 0);
            add(comboBox_1, gbc);

            JButton btnNewButton = new JButton("Go");
            btnNewButton.setBorder(new BevelBorder(BevelBorder.RAISED, new Color(0, 0, 255), null, Color.BLUE, null));
            btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 13));
            btnNewButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    if (comboBox_1.getSelectedItem().toString().equals("Student")) {

                        JOptionPane.showMessageDialog(null, "You can  Login !", "", JOptionPane.ERROR_MESSAGE);
                        //new Register_Student().setVisible(true); 
                    }
                }
            });
            gbc.insets = new Insets(50, 0, 50, 0);
            gbc.ipady = 0;
            add(btnNewButton, gbc);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(1000, 600);
        }

        private javax.swing.JComboBox<String> comboBox_1;
    }
}