0

我试图通过比较表中的数据和来自 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);
        }

    }
4

1 回答 1

1

您将 null 作为输入传递给:

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

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

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

每当您尝试在空对象上调用方法时,您都会收到 NullPointerException

于 2013-02-27T14:33:52.717 回答