使用WHERE子句加载JCombobox和数据

时间:2013-02-27 14:22:31

标签: java sql arrays swing jdbc

我试图通过比较数据表和来自myframe的数据来加载到组合框,但我的代码运行时我得到了这个错误消息: - java.lang.NullPointerException,我也尝试使用向量。请帮助:这是我的数据库类的代码

    public ArrayList allocateStaffcombobox(Allocation aloc) throws SQLException
{
    ArrayList<Allocation> vec = new ArrayList<Allocation>();
    String sql = "select * from staffsubalocation where SubCode='"+aloc.getSubjCode()+"'";

    ResultSet result = stmt.executeQuery(sql);

    while (result.next())
    {

        String staffNo = result.getString("StaffNo");

        String subcode=result.getString("SubCode");
        System.out.println(staffNo+" "+ subcode);
        vec.add(new Allocation(staffNo,subcode));
    }

    return vec;

}

这是我的java类的代码:

     public  void loadStaffCombo()
    {
        try
        {
            DatabaseManager db = new DatabaseManager();
            ArrayList<Allocation> sub=db.allocateStaffcombobox(null);
            Staffcombobox.removeAllItems();
           // String firsIndex = " ";
            for(int x = 0; x< sub.size(); x++)
            {
                Staffcombobox.addItem( sub.get(x).getStaffNo());
            }

        }
        catch (SQLException ex)
        {
            Logger.getLogger(ViewSubjectsJInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

1 个答案:

答案 0 :(得分:1)

您将null作为输入传递给:

ArrayList<Allocation> sub=db.allocateStaffcombobox(null);

您尝试在以下位置取消引用null:

String sql = "select * from staffsubalocation where SubCode='"+aloc.getSubjCode()+"'";

每当您尝试在null对象上调用方法时,您将获得NullPointerException