This question already has an answer here:
I'm not too sure what's going on here. I've used nearly the same code multiple times to achieve the same purposes in other tables in the same class this one is written. Essentially I'm listing information in rows in a JTable, and the first column is a checkbox that allows users to select or deselect it before clicking a button to take them to the next page. That checkbox works fine (table_3). However, a similar table is on the page it brings the user too. Rows are added to the table in the next page once the button is pressed. In the first column again I have a checkbox like before. However, once the button to bring the user to the page is clicked, I get a Null Pointer Exception for the new JTable being generated (table_5).
Here is the Code for the button (scroll down to see where tableModel_5 is used):
JButton btnEditCadet = new JButton("Edit Cadet");
btnEditCadet.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
for (int n = 0; n < tableModel_3.getRowCount(); n++)
{
if((boolean) (tableModel_3.getValueAt(n, 0)))
{
edPlace=n;
tableModel_3.setValueAt(false, n, 0);
n = tableModel_3.getRowCount(); //Edits only the first selection it comes across
}
}
textFirstNameEd.setText(cumulativeCadetList.getCadets().get(edPlace).getFirstName());
textLastNameEd.setText(cumulativeCadetList.getCadets().get(edPlace).getLastName());
textFlightEd.setText(cumulativeCadetList.getCadets().get(edPlace).getFLight());
textYearEd.setText(cumulativeCadetList.getCadets().get(edPlace).getYear());
if(edPlace >= 0)
{
for (int i = 0; i < cumulativeCadetList.getCumulativeEventList().getEvents().size(); i++)
{
String type;
if(cumulativeCadetList.getCumulativeEventList().getEvents().get(i).getType())
type = "CS";
else
type = "EV";
Calendar stTime = new GregorianCalendar();
stTime.setTime(cumulativeCadetList.getCumulativeEventList().getEvents().get(i).getStartTime());
Calendar endTime = new GregorianCalendar();
endTime.setTime(cumulativeCadetList.getCumulativeEventList().getEvents().get(i).getEndTime());
Calendar evDate = new GregorianCalendar();
evDate.setTime(cumulativeCadetList.getCumulativeEventList().getEvents().get(i).getDate());
//To-DO Add date comparison
boolean inEvent = false;
for (int j = 0; j < cumulativeCadetList.getCadets().get(edPlace).getCadetEvents().size(); j++)
if (cumulativeCadetList.getCadets().get(edPlace).getCadetEvents().get(j).getName().equals(cumulativeCadetList.getCumulativeEventList().getEvents().get(i).getName()))
inEvent = true;
//Line where tableModel_5 is accessed
tableModel_5.addRow(new Object[]{
inEvent, //Changing inEvent to false or true does not remove error
(evDate.get(Calendar.MONTH)+1) + "/" + evDate.get(Calendar.DAY_OF_MONTH) + "/" + evDate.get(Calendar.YEAR),
type,
cumulativeCadetList.getCumulativeEventList().getEvents().get(i).getSpecial(),
cumulativeCadetList.getCumulativeEventList().getEvents().get(i).getName(),
stTime.get(Calendar.HOUR_OF_DAY) + ":" + stTime.get(Calendar.MINUTE) + " - " + endTime.get(Calendar.HOUR_OF_DAY) + ":" + endTime.get(Calendar.MINUTE),
cumulativeCadetList.getCumulativeEventList().getEvents().get(i).getCadets().size()
});
}
cardPanel.show(panel, "editCadet");
}
}
});
Here's the code where the null pointer exception is called:
EditCadet.setLayout(gl_EditCadet);
table_5 = new JTable(tableModel_5){
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass(); //Null pointer on this line
}};
scrollPane_5.setViewportView(table_5);
However, I use nearly identical code with table_3 with no issue as seen here:
JButton btnEditCadets = new JButton("Edit Cadets");
btnEditCadets.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
cardPanel.show(panel, "editCadets");
for (int i = 0; i < cumulativeCadetList.getCadets().size(); i++)
{
if(tableModel_3.getRowCount() == 0)
{
tableModel_3.addRow(new Object[]
{
false,
cumulativeCadetList.getCadets().get(i).getFLight(),
cumulativeCadetList.getCadets().get(i).getYear(),
cumulativeCadetList.getCadets().get(i).getLastName(),
cumulativeCadetList.getCadets().get(i).getFirstName(),
cumulativeCadetList.getCadets().get(i).getCSHours(),
cumulativeCadetList.getCadets().get(i).getEVHours(),
cumulativeCadetList.getCadets().get(i).getCSHours()+cumulativeCadetList.getCadets().get(i).getEVHours()
});
}
With no null pointer called from:
EditCadets.setLayout(gl_EditCadets);
table_3 = new JTable(tableModel_3){
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}};
scrollPane_3.setViewportView(table_3);
I've been looking at this for hours trying tons of things, but can't figure out what's going on. If anyone has any ideas of things to try, I would be most greatful!
Thank you.
Aucun commentaire:
Enregistrer un commentaire