【问题标题】:Loading JCombobox with data selected using a WHERE clause使用 WHERE 子句选择的数据加载 JCombobox
【发布时间】:2013-02-27 14:22:31
【问题描述】:

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

    }

【问题讨论】:

  • 你能发布错误堆栈跟踪吗?
  • 您在loadStaffCombo 方法中的声明ArrayList&lt;Allocation&gt; sub = db.allocateStaffcombobox(null); 中明确传递null,这肯定会给您一个NullPointerException
  • 我该如何解决这个问题(在该语句中不传递 null

标签: java sql arrays swing jdbc


【解决方案1】:

您将 null 作为输入传递给:

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

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

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

当你尝试调用一个空对象的方法时,你会得到一个 NullPointerException

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    • 2011-03-18
    • 1970-01-01
    • 2014-03-14
    • 2019-04-10
    • 1970-01-01
    • 2015-11-16
    相关资源
    最近更新 更多