【问题标题】:jCombobox NullPointer exceptionjCombobox NullPointer 异常
【发布时间】:2016-07-27 10:16:35
【问题描述】:

我正在创建一个包含 3 个组合框的框架,每个组合框都依赖于另一个,第 3 个依赖于第 2 个,第 2 个依赖于第 1 个。 问题是当我更改第一个时,我在第三个得到 NullPointer 异常,因为它是第二个的更改动作。

我的问题是当我更改第一个 jComboBox "jComboBox0" 时,如何防止第三个 jComboBox "jCombobox2" 上的项目更改操作?

这是我的代码:

    private void jComboBox0ItemItemStateChanged(ItemEvent event) {
    jComboBox1.removeAllItems();
    ComboItem cat = (ComboItem) jComboBox0.getSelectedItem();


    String requete = "from Subcategory where Fk_Category = " + cat.getValue();

    Collection subcategories = Subcategory.getListeSubcategory(requete);

    for (Iterator i = subcategories.iterator(); i.hasNext();) {
        Subcategory item = new Subcategory();
        item = (Subcategory) i.next();
        System.out.println(item.getId());

        jComboBox1.addItem(new ComboItem(item.getNom(), (int) item.getId()));
    }

    // System.out.println("tbdlat a lkhra ! : "+listCategory.get(0));

}

private void jComboBox1ItemItemStateChanged(ItemEvent event) {
    // nda2
    jComboBox2.removeAllItems();
    ComboItem cat = (ComboItem) jComboBox1.getSelectedItem();


    String requete = "from Area  where fk_Subcategory = " + cat.getValue()+" group by Nom_Area";

    Collection areas = Area.getListeArea(requete);

    for (Iterator i = areas.iterator(); i.hasNext();) {
        Area item = new Area();
        item = (Area) i.next();
        System.out.println(item.getId());

        jComboBox2.addItem(new ComboItem(item.getNom(), (int) item.getId()));
    }

}

private void jComboBox2ItemItemStateChanged(ItemEvent event) {
    // i'll do some code here
}

【问题讨论】:

标签: java swing nullpointerexception jframe jcombobox


【解决方案1】:

您的代码的问题似乎是您没有检查是否选择了元素。如果选择了一个项目,您应该检查您的侦听器内部,如果您不检查,itemStateChange 方法将被调用两次。

我在下面发布了一个代码,其中包含三个组合框和一个侦听器,用于所有三个组合:

    import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;

public class MainWindow1 extends JFrame implements ItemListener {

    public MainWindow1() {

        String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };

        //Create the combo box, select item at index 4.
        //Indices start at 0, so 4 specifies the pig.

        setLayout(new FlowLayout());

        JComboBox petList1 = new JComboBox(petStrings);
        //petList1.setSelectedIndex(4);
        petList1.addItemListener(this);
        petList1.setName("petList1");

        JComboBox petList2 = new JComboBox(petStrings);
        petList2.setSelectedIndex(4);
        petList2.addItemListener(this);
        petList2.setName("petList2");

        JComboBox petList3 = new JComboBox(petStrings);
        petList3.setSelectedIndex(4);
        petList3.addItemListener(this);
        petList3.setName("petList3");

        getContentPane().add(petList1);
        getContentPane().add(petList2);
        getContentPane().add(petList3);


        setSize(800, 600);
        setVisible(true);
    }

    public static void main(String[] args) {
        new MainWindow1();

    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e != null && e.getSource().toString() != null && e.getSource().toString().contains("petList1") && e.getStateChange() == ItemEvent.SELECTED) {

            System.out.println( "1" + e.getSource());
        } else if (e != null && e.getSource().toString() != null && e.getSource().toString().contains("petList2") && e.getStateChange() == ItemEvent.SELECTED) {
            System.out.println( "2" + e.getSource());
        } else if (e != null && e.getSource().toString() != null && e.getSource().toString().contains("petList3") && e.getStateChange() == ItemEvent.SELECTED) {

            System.out.println("3" + e.getSource());
        }

    }

}

【讨论】:

  • 正是我今天早上通过检查jcombobox是否为空解决了这个问题
猜你喜欢
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多