0

调用方法 fillDBWeek(String mName) 时出现 NullPointerException。我无法发现我哪里出错了,有人可以帮我吗?

这是我为以下方法得到的错误

*Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at View.LectureReport.fillDBWeek(LectureReport.java:49)
    at View.LectureReport$2.actionPerformed(LectureReport.java:96)*

方法 fillDBWeek

public void fillDBWeek(String mName)
    {
         tableModel.setDataVector(lRHand.popuSDataWeek(mName), columnNames);
         table.setModel(tableModel);
         table.repaint(); 
    }

按下按钮时调用该方法的位置

JButton btnViewReport = new JButton("View Report");
    btnViewReport.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String moduleName = (String)comboBox.getSelectedItem();
            if(comboBox_1.getSelectedItem().equals("Week"))
            {
                fillDBWeek(moduleName);
            }
            else if (comboBox_1.getSelectedItem().equals("Semester"))
            {
                fillDBSem(moduleName);
            }
            else if (comboBox_1.getSelectedItem().equals("Year"))
            {
                fillDBYear(moduleName);
            }
        }
    });

查询用于调用 SQL 数据库中的数据

public Object[][] popuSDataWeek(String moduleName)
{
    data = new Object[30][9];

    try{
           Class.forName(DRIVER_CLASS);

            connection = DriverManager.getConnection( url,"polk", "mire");

            statement = connection.createStatement();
            results = statement.executeQuery("SELECT * FROM Group6_StudAttWeek WHERE module = '"+ moduleName +"'");
            int i=0;
            while(results.next())
            {
            data[i][0]= results.getString("firstname");
            data[i][1]= results.getString("lastname");
            data[i][2]= results.getString("module");
            data[i][3] = results.getString("yearOfStudy");
            data[i][4]= results.getInt("workshop%");
            data[i][5]= results.getInt("tutorial%");
            data[i][6] = results.getInt("lecture%");
            data[i][7] = results.getInt("avg%");
            data[i][8] = results.getInt("number");
            i++;
            }
       results.close();
       statement.close();
       connection.close();
       } catch (SQLException sqlException) {
       sqlException.printStackTrace();
       System.exit(1);
       }catch(Exception exception) {
       System.err.println("An error happened");
       System.exit(1);
       }

    return data;
}
4

1 回答 1

1

基于堆栈:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at View.LectureReport.fillDBWeek(LectureReport.java:49)
    at View.LectureReport$2.actionPerformed(LectureReport.java:96)

异常上升fillDBWeek,要么是因为tableModel要么table为空。相反,如果异常由 引发popuSDataWeek,它应该出现在堆栈跟踪中,但无论如何我看到的唯一可能的空对象是connection.

请确保在使用它们之前声明并初始化了正确的对象。

于 2012-12-04T16:23:39.610 回答