调用方法 fillTable() 时,我似乎无法发现出现 NullPointerException 错误的位置。fillTable 是从按钮调用的,它从文本字段中获取字符串。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;
}
在其他课程中使用相同的技术,它们似乎有效。