NullPoInterException:无法调用“ Jcombobox.getSelectedIndex()”因为&quot this.this $ 0.combobox_1&quot是无效的

发布于 2025-01-28 19:17:58 字数 3469 浏览 2 评论 0原文

我的组合框总是返回零值。在我的项目“出勤管理系统”中,我需要在组合框中验证所选项目,但它返回了空。如何使其从组合框中返回选择的项目?

我使用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;
}

My combo box always returns null value. In my Project "Attendance Management System", I need to validate the selected item in the combo box, but it returns null. How can I get it to return the item selected from the combo box?

I use Eclipse IDE to run my project.

Here is my code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗫嚅 2025-02-04 19:17:58

您创建了两个组合框对象。请删除顶部或jcombobox combobox_1 = to combobox_1 =中的Combobox声明。

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

You have created two combo box objects. Please remove the ComboBox declaration on the top or JComboBox comboBox_1 = to comboBox_1 = in the constructor.

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;
}
蘸点软妹酱 2025-02-04 19:17:58

您正在阴影变量。也就是说,您已经声明了combobox_1两次,一次是一个实例字段,一次是局部变量...

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

现在摆脱本地法令

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

因此,当我做nead时, admin()。setVisble(true)显示哪个帧?

作为一般建议,您应该避免直接从jframe等顶级容器中延伸,您不会在类中添加任何新功能,并且将自己限制在单个用例中。

而且,您真的,真的,真的很想学习如何使用布局经理,请参阅在容器中布置组件有关更多详细信息

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

You're shadowing your variables. That is, you've declared comboBox_1 twice, once as an instance field and once as a local variable...

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

Get rid of the local decleration

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

Now, having said that, you have another issue. admin extends from JFrame...

public class admin extends javax.swing.JFrame {

But then you create a second JFrame in the constructor...

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

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

So, when I do new admin().setVisble(true) which frame is getting displayed?

As a general recommendation, you should avoid extending directly from top level containers like JFrame, you're not adding any new functionality to the class and you're restricting yourself to a single use case.

And, you really, really, really want to learn how to use layout managers, see Laying Out Components Within a Container for more details

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文