首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jCombobox NullPointer异常

jCombobox NullPointer异常
EN

Stack Overflow用户
提问于 2016-07-27 18:16:36
回答 1查看 1.2K关注 0票数 0

我正在创建一个包含3个组合框的框架,每个组合框相互依赖,第三个依赖于第二个,第二个依赖于第一个。问题是,当我更改第一个操作时,我在第三个获得了NullPointer异常,因为它的操作是第二个操作的更改操作。

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

下面是我的代码:

代码语言:javascript
复制
    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
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-28 04:28:39

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

我在下面发布了一段代码,它有三个组合框和一个监听器,用于所有三个组合框:

代码语言:javascript
复制
    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());
        }

    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38610117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档