使用多个组合框选择时出现空指针异常错误

时间:2018-08-29 05:02:33

标签: java nullpointerexception

我将3个组合框添加到根据第一个组合框的选择更改2个组合框的形式。这里,当选择一个类别时,子类别将加载到一个组合框,尺寸将加载到下一个组合框。第一个组合框选择中的编码如下所示

    private void jCcatItemStateChanged(java.awt.event.ItemEvent evt) {                                       
    //jlsubtype.removeAll();
    System.out.println(jCcat.getSelectedItem().toString());
    if (jCcat.getSelectedItem().toString().matches("Uniform")) {
        jCsubCat.removeAllItems();
        jCsubCat.addItem("Long Sleeve");
        jCsubCat.addItem("Short Sleeve");
        jCsubCat.addItem("Overcoat");
        jCsubCat.addItem("Overoll");
    } else if (jCcat.getSelectedItem().toString().matches("Headwear")) {
        jCsubCat.removeAllItems();
        jCsubCat.addItem("Caps");
        jCsubCat.addItem("Scaffs");
    } else if (jCcat.getSelectedItem().toString().matches("Footwear")) {
        jCsubCat.removeAllItems();
        jCsubCat.addItem("Shoes");
        jCsubCat.addItem("Slippers");
    }
    if (jCcat.getSelectedItem().toString().matches("Uniform")) {
        jCsSize.removeAllItems();
        jCsSize.addItem("XS");
        jCsSize.addItem("S");
        jCsSize.addItem("M");
        jCsSize.addItem("L");
        jCsSize.addItem("XL");
        jCsSize.addItem("XXL");
        jCsSize.addItem("XXXL");
    } else if (jCcat.getSelectedItem().toString().matches("Headwear")) {
        jCsSize.removeAllItems();
        jCsSize.addItem("Free Size");

    } else if (jCcat.getSelectedItem().toString().matches("Footwear")) {
        jCsSize.removeAllItems();
        jCsSize.addItem("35");
        jCsSize.addItem("36");
        jCsSize.addItem("37");
        jCsSize.addItem("38");
        jCsSize.addItem("39");
        jCsSize.addItem("40");
        jCsSize.addItem("4");
        jCsSize.addItem("5");
        jCsSize.addItem("6");
        jCsSize.addItem("7");
        jCsSize.addItem("8");
        jCsSize.addItem("9");
    }
}                     

*然后更改第二个组合框的类型,将其加载到列表框中。该代码如下所示

    private void jCsubCatItemStateChanged(java.awt.event.ItemEvent evt) {                                          

    if (jCcat.getSelectedItem().toString().matches("Uniform")) {
        DefaultListModel lists = new DefaultListModel();
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            //Connection con = DriverManager.getConnection("jdbc:sqlserver://10.228.59.2:1433;databaseName=uniforminventorydb;user=SA;password=KreedaIntimo@2017;");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/uniforminventorydb", "root", "");
            Statement st = con.createStatement();
            PreparedStatement pst;

            String Catagory;
            String Type;

            Catagory = jCcat.getSelectedItem().toString();
            Type = jCsubCat.getSelectedItem().toString();

            String sql = "Select Distinct SubType from add_stock Where Catergory = '" + Catagory + "' and Type = '" + Type + "'";
            pst = con.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();

            while (rs.next()) {

                System.out.println(rs.getString(1));
                String ls = rs.getString(1);
                lists.addElement(ls);

                jLsType.setModel(lists);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if (jCcat.getSelectedItem().toString().matches("Footwear")) {
        DefaultListModel lists = new DefaultListModel();
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            //Connection con = DriverManager.getConnection("jdbc:sqlserver://10.228.59.2:1433;databaseName=uniforminventorydb;user=SA;password=KreedaIntimo@2017;");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/uniforminventorydb", "root", "");
            Statement st = con.createStatement();
            PreparedStatement pst;

            String Catagory;
            String Type;

            //Catagory = jCcat.getSelectedItem().toString();
            Catagory = "Footwear";

            Type = jCsubCat.getSelectedItem().toString();

            String sql = "Select Distinct SubType from add_stock Where Catergory = '" + Catagory + "' and Type = '" + Type + "'";
            pst = con.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();

            while (rs.next()) {

                System.out.println(rs.getString(1));
                String ls = rs.getString(1);
                lists.addElement(ls);

                jLsType.setModel(lists);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    } else if (jCcat.getSelectedItem().toString().matches("Headwear")) {
        DefaultListModel lists = new DefaultListModel();
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            //Connection con = DriverManager.getConnection("jdbc:sqlserver://10.228.59.2:1433;databaseName=uniforminventorydb;user=SA;password=KreedaIntimo@2017;");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/uniforminventorydb", "root", "");
            Statement st = con.createStatement();
            PreparedStatement pst;

            String Catagory;
            String Type;

            // Catagory = jCcat.getSelectedItem().toString();
            Catagory = "Headwear";
            //Type = jCsubCat.getSelectedItem().toString();

            String sql = "Select Distinct SubType from add_stock Where Catergory = '" + Catagory + "'";
            pst = con.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();

            while (rs.next()) {

                System.out.println(rs.getString(1));
                String ls = rs.getString(1);
                lists.addElement(ls);

                jLsType.setModel(lists);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

将类别更改为鞋类后运行此代码时,会出现以下错误

     java.lang.NullPointerException
at uniforminventorysystem.HomeAdmin.jCsubCatItemStateChanged(HomeAdmin.java:2068)
at uniforminventorysystem.HomeAdmin.access$600(HomeAdmin.java:47)

ps.i已为第一个组合框加载了项,其方法如下:

//选择分类方法

    private void catergorySelect(){     
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        //Connection con = DriverManager.getConnection("jdbc:sqlserver://10.228.59.2:1433;databaseName=uniforminventorydb;user=SA;password=KreedaIntimo@2017;");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/uniforminventorydb", "root", "");
        String sql = "select * from catergoryLoad";
        pst = con.prepareStatement(sql);
        ResultSet r = pst.executeQuery();

        while (r.next()) {

            jCcat.addItem(r.getString("Catergory"));
            jCrcat3.addItem(r.getString("Catergory"));
            jCocat.addItem(r.getString("Catergory"));
        }

        con.close();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Failed to Connect to Database", "Error Connection", JOptionPane.WARNING_MESSAGE);
        System.exit(0);
    }

}

请给我解决这个问题的方法

0 个答案:

没有答案