调用方法时出现NullPointerException

时间:2012-12-04 15:14:39

标签: java mysql eclipse nullpointerexception jcombobox

调用方法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;
}

1 个答案:

答案 0 :(得分: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中的异常上升,原因是tableModeltable为空。如果popuSDataWeek抛出异常,它应该出现在堆栈跟踪中,但无论如何,我看到的唯一可能的空对象是connection

请确保在使用之前声明和初始化正确的对象。