简体   繁体   English

调用方法时发生NullPointerException

[英]NullPointerException when calling a method

Getting a NullPointerException when calling method fillDBWeek(String mName). 调用方法fillDBWeek(String mName)时获取NullPointerException。 I can not spot where I am going wrong can some one please help me? 我无法发现我要去哪里错了,有人可以帮助我吗?

This is the error I am getting for the following method 这是我为以下方法得到的错误

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

Method fillDBWeek 方法fillDBWeek

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

Where the method is called when pressing button 按下按钮时调用方法的位置

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);
            }
        }
    });

Query using to call the data from SQL database 查询用于从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;
}

Based on the stack: 基于堆栈:

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

The exception rises in fillDBWeek , either because tableModel or table are null. 由于tableModeltable为null,在fillDBWeek中会fillDBWeek异常。 If the exception is thrown by popuSDataWeek , instead, it should be present on the stacktrace, but anyway the only probable null object I see is connection . 如果异常是由popuSDataWeek引发的,那么它应该出现在stacktrace上,但是无论如何,我看到的唯一可能的null对象是connection

Please, make sure the proper objects are declared and initialized before using them. 请在使用它们之前确保正确的对象已声明并初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM