NullPointerException调用fillTable()时出错

时间:2012-12-03 16:22:23

标签: java mysql swing nullpointerexception jtable

我似乎无法发现我在哪里获得NullPointerException调用方法fillTable()时出错。从按钮调用fillTable从textField获取String。 fillTable用于通过检索包含处理程序类的数据的Object [] []来填充JTable。

private void fillTable(String dataType, String searchBy)
    {
        String[] columnName = {"module name", "Workshop %", "Lecture %", "Tutorial %", "Average %"};
        if(searchBy.startsWith("@"))
        {
            tableModel.setDataVector(pLSHand.gIStudData(dataType, searchBy), columnName);
            table.setModel(tableModel);
        }
        else
        {
            tableModel.setDataVector(pLSHand.gNStudData(dataType, searchBy), columnName);
            table.setModel(tableModel); 
        }
        table.repaint();
    }

按钮:

String searchBy = (String)textField.getText();
if(!searchBy.isEmpty())
{
   fillTable("Year",searchBy);
}
else
{

}

Handler类中的方法

public Object[][] gIStudData(String dType, String studID)
    {

        String rQuery = "SELECT * FROM StudAttYear WHERE studentID = '"+ studID +"'";
        data = new Object[1][5];

        if(dType.equalsIgnoreCase("Semester"))
        {
            rQuery = "SELECT * FROM StudAttSem WHERE studentID = '"+ studID +"'";
            data = new Object[2][5];
        }
        else if(dType.equalsIgnoreCase("Year"))
        {
            rQuery = "SELECT * FROM StudAttYear WHERE studentID = '"+ studID +"'";
            data = new Object[1][5];
        }
        else if(dType.equalsIgnoreCase("Week"))
        {
            rQuery = "SELECT * FROM StudAttWeek WHERE studentID = '"+ studID +"' AND number = '"+ weekNo +"'";
            data = new Object[12][5];
        }

        try{
               Class.forName(DRIVER_CLASS);

                connection = DriverManager.getConnection( url,"agile", "adila");
                statement = connection.createStatement();
                results = statement.executeQuery(rQuery);
                int i=0;
                while(results.next())
                {
                     data[i][0] = results.getString("module");
                     data[i][1] = results.getString("workshop%");
                     data[i][2] = results.getString("tutroial%");
                     data[i][3] = results.getString("lecture%");
                     data[i][4] = results.getString("avg%");
                 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)

如上所述,pLSHand未在您的代码中的其他地方声明。