[英]Adding an String array to a JComboBox
My Aim is to get a Value from an SQL table and add the value to a combobox 我的目标是从SQL表中获取一个值并将该值添加到组合框
Connection con;
Statement st;
ResultSet rs;
con=DriverManager.getConnection("jdbc:mysql://localhost/needed","arv","");
st=con.createStatement();
rs= st.executeQuery("SELECT * FROM INVENTORY");
List<String> l = new ArrayList();
while(rs.next()){
l.add(rs.getString("CAT"));
}
String[] h = l.toArray(new String[l.size()]);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(h));
But I'm not able to execute it since im getting a NPE at jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(h)); 但是我无法执行它,因为我在jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(h))上获得了NPE。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at FilterTabs.<init>(FilterTabs.java:43)
How do i proceed after this and how should i solve this NPE? 此后我该如何进行?应该如何解决该NPE?
Give this code snippet, the only thing that can be null in the expression jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(h));
提供此代码段,这是表达式
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(h));
唯一可以为null的东西jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(h));
is the jComboBox1
. 是
jComboBox1
。
Make sure you initialize it before reaching this code snippet. 在到达此代码段之前,请确保将其初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.