组合框删除所有项目后无法单击

时间:2014-04-28 12:54:33

标签: java sql-server swing nullpointerexception jcombobox

我正在创建一个对话框,用项目代码列表填充组合框,当我单击组合框上的特定项时,它会从数据库中获取信息并使用jLabel显示在同一对话框中。有时我想禁用组合框我执行removeAllItems并设置jLabels空字符串以及复选框并继续下一步。当我再次启用时,我使用相同的对象填充组合框,但是当我单击它时,它不会单击并创建错误。当我为removeAllItems编码时,它工作正常。

这是我的代码片段

//我的对话类

/// Fields 

    MsSqlDbProperties searchcomboitem;
    MsSqlDbProperties objcombo;
    String combos;
    Object comboitem ;

///我的对话框的构造函数从另一个类

调用
  public myDialog(java.awt.Frame parent, boolean modal) {
   super(parent, modal);
   initComponents();
   objcombo = new MsSqlDbProperties();
   objcombo.populatecombo(this);
  }

//单击组合框项目

      private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
   System.out.println("Combo Item Selected");
    comboitem = jComboBox2.getSelectedItem();
    combos = comboitem.toString();
    System.out.println(combos);
    objcombo.SearchProject(this, combos);
}  

//禁用组合框

private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           

if (jComboBox2.isEnabled()) {

jComboBox2.setEnabled(false);
this.jComboBox2.removeAllItems();
System.out.println(" Combo Disabled...");

 }    else {

System.out.println(" Combo Enabled...");
jComboBox2.setEnabled(true);
objcombo.populatecombo(this);

}

 }  

// MyDBproperties Class

 /// Assume Connection Made here

/// my Search Project Method 


public boolean SearchProject(PurchaseDialog pd , String s) {


    boolean resp = false;
    searchcon = connector();

    try {

        String Sqlse = "SELECT * FROM ProjectAccount WHERE ProjectCode = ? ";
        PreparedStatement searchcmd = searchcon.prepareStatement(Sqlse);
        searchcmd.setString(1,s);

       ResultSet rst = searchcmd.executeQuery();

        if (rst.next()) {
            String add1 = rst.getString("ProjectName");

            pd.projectName.setText(add1);
            System.out.println("AFTER DISABLE");

        System.out.println(add1);
        String add2 = rst.getString("ProjectCode");
        pd.projectCodelabel.setText(add2);
        System.out.println(add2);

        String add3 = rst.getString("StartDate");
        pd.StartDate.setText(add3); 
        System.out.println(add3);

        String add4 = rst.getString("EndDate");
        pd.EndDate.setText(add4);
        System.out.println(add4);

        }

        searchcmd.close();
        searchcon.close();

        resp = true;

    } catch (Exception ex) {

        System.out.println("Error " + ex.getMessage());

    }

    return resp;
}  

//我的填充组合框方法

 public boolean populatecombo(PurchaseDialog pdc) {

  System.out.println("in populatecombo");
  boolean resp = false;
  populatecon = connector();

try {
    String Sqlsc = "SELECT ProjectCode FROM ProjectAccount";
    PreparedStatement combocmd = populatecon.prepareStatement(Sqlsc);
    ResultSet rsc = combocmd.executeQuery();

    while (rsc.next()) {
        pdc.jComboBox2.addItem(rsc.getString("ProjectCode"));

    }

    combocmd.close();
    populatecon.close();

} catch (Exception ex) {
    System.out.println("Error " + ex.getMessage());
}

return resp;

}

这是错误列表

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at
    ptakip.PurchaseDialog.jComboBox2ActionPerformed(PurchaseDialog.java:493) at 
    ptakip.PurchaseDialog.access$100(PurchaseDialog.java:14) at
    ptakip.PurchaseDialog$2.actionPerformed(PurchaseDialog.java:148) at
    javax.swing.JComboBox.fireActionEvent(JComboBox.java:1260) at
    javax.swing.JComboBox.contentsChanged(JComboBox.java:1331) at
    javax.swing.JComboBox.intervalRemoved(JComboBox.java:1351) at
    javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:179) at
    javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:174) at 
    javax.swing.JComboBox.removeAllItems(JComboBox.java:773) at
    ptakip.PurchaseDialog.jCheckBox1ActionPerformed(PurchaseDialog.java:510) at 
    ptakip.PurchaseDialog.access$300(PurchaseDialog.java:14) at
    ptakip.PurchaseDialog$4.actionPerformed(PurchaseDialog.java:165) at
    javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at
    javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at
    javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at
    javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:308) at
    javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6505) at
    javax.swing.JComponent.processMouseEvent(JComponent.java:3320) at
    java.awt.Component.processEvent(Component.java:6270) at
    java.awt.Container.processEvent(Container.java:2229) at
    java.awt.Component.dispatchEventImpl(Component.java:4861) at
    java.awt.Container.dispatchEventImpl(Container.java:2287) at
    java.awt.Component.dispatchEvent(Component.java:4687) at
    java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at
    java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at 
    java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at 
    java.awt.Container.dispatchEventImpl(Container.java:2273) at
    java.awt.Window.dispatchEventImpl(Window.java:2719) at
    java.awt.Component.dispatchEvent(Component.java:4687) at
    java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at
    java.awt.EventQueue.access$200(EventQueue.java:103) at
    java.awt.EventQueue$3.run(EventQueue.java:694) at
    java.awt.EventQueue$3.run(EventQueue.java:692) at
    java.security.AccessController.doPrivileged(Native Method) at
    java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at
    java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at 
    java.awt.EventQueue$4.run(EventQueue.java:708) at
    java.awt.EventQueue$4.run(EventQueue.java:706) at
    java.security.AccessController.doPrivileged(Native Method) at
    java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at 
    java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at
    java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at
    java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at
    java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:154) at 
    java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:182) at
    java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at
    java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733) at
    java.awt.EventQueue.access$200(EventQueue.java:103) at
    java.awt.EventQueue$3.run(EventQueue.java:694) at
    java.awt.EventQueue$3.run(EventQueue.java:692) at
    java.security.AccessController.doPrivileged(Native Method) at
    java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at 
    java.awt.EventQueue.dispatchEvent(EventQueue.java:703) at
    java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at
    java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at
    java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at
    java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at 
    java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

1 个答案:

答案 0 :(得分:3)

在删除所有项目后单击jComboBox2时,所选项目将为null。请在执行活动之前检查空值或项目计数。示例代码

private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt)         
{                                           
    System.out.println("Combo Item Selected");
    comboitem = jComboBox2.getSelectedItem();
    if(null==comboitem)
       return ;

    combos = comboitem.toString();
    System.out.println(combos);
    objcombo.SearchProject(this, combos);
}