Getting a NullPointerException when calling method fillDBWeek(String mName). 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
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
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:
The exception rises in
fillDBWeek, either becausetableModelortableare null. If the exception is thrown bypopuSDataWeek, instead, it should be present on the stacktrace, but anyway the only probable null object I see isconnection.Please, make sure the proper objects are declared and initialized before using them.