NullPoInterException:无法调用“ Jcombobox.getSelectedIndex()”因为&quot this.this $ 0.combobox_1&quot是无效的
我的组合框总是返回零值。在我的项目“出勤管理系统”中,我需要在组合框中验证所选项目,但它返回了空。如何使其从组合框中返回选择的项目?
我使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您创建了两个组合框对象。请删除顶部或
jcombobox combobox_1 =
tocombobox_1 =
中的Combobox声明。You have created two combo box objects. Please remove the ComboBox declaration on the top or
JComboBox comboBox_1 =
tocomboBox_1 =
in the constructor.您正在阴影变量。也就是说,您已经声明了
combobox_1
两次,一次是一个实例字段,一次是局部变量...现在摆脱本地法令
,这就是说,您还有另一个问题。
admin
从jframe
...但是,您在构造函数中创建第二个
jframe
...因此,当我做
nead时, admin()。setVisble(true)
显示哪个帧?作为一般建议,您应该避免直接从
jframe
等顶级容器中延伸,您不会在类中添加任何新功能,并且将自己限制在单个用例中。而且,您真的,真的,真的很想学习如何使用布局经理,请参阅在容器中布置组件有关更多详细信息
You're shadowing your variables. That is, you've declared
comboBox_1
twice, once as an instance field and once as a local variable...Get rid of the local decleration
Now, having said that, you have another issue.
admin
extends fromJFrame
...But then you create a second
JFrame
in the constructor...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