This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12个答案)
四年前关闭。
当调用fillTable()方法时,我似乎找不到我在哪里得到NullPointerException错误。fillTable是从按钮调用的,它从文本字段获取字符串。
fillTable旨在通过从处理程序类检索包含数据的对象[][]来填充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
{

}

处理程序类中的方法
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;
    }

在其他类中使用相同的技术,它们似乎可以工作。

最佳答案

如前所述,pLSHand不会在代码的其他地方声明。

07-24 09:19