2017-07-02 20 views
-1
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {           

     jComboBox1.revalidate(); 
     jComboBox2.removeAllItems(); 
     jComboBox3.removeAllItems(); 
     jComboBox4.removeAllItems(); 
     String b1=jComboBox1.getSelectedItem().toString(); 
     String bb=this.branch; 
     String y1=this.year; 
      if(y1!=null){ 
       String[] b=y1.split(";"); 
       System.out.println(y1); 
       System.out.println(b1); 
       int size=b.length; 
       System.out.println(size); 
       for(int i=0;i<size;i++){ 
       if(b[i].matches("(?i).*"+b1+".*")){ 
        System.out.println(b[i]); 
       jComboBox2.addItem(b[i].replaceAll(":","").replaceAll(b1.toLowerCase(), "")); 
      jComboBox2ActionPerformed(evt); 
      }}} 
    } 

它先运行时间,同时加载框架,但是当我选择谢胜利项目它给了我错误它先运行时间,同时加载框架,但是当我选择谢胜利项目它给我的错误

+5

什么是错误?你可以在帖子中显示堆栈跟踪吗? – DevilsHnd

回答

0

一旦你适用removeAllItems那么在组合框中没有项目

所以getSelectedItem内部使用的ComboBoxModel getSelectedItem函数声明

选定的项目或NULL,如果再没有选择

虽然oracle docs没有说明任何关于返回null

解决方案:你在null得到nulltoString()因为你的空指针异常所以一定要确保你有一些项目,当你调用getSelectedItem或把无效检查

String b1=jComboBox1.getSelectedItem()!=null ? jComboBox1.getSelectedItem().toString(): ""; 
相关问题