I’m having a hard time with this and I’m a beginner so please yell at my bad programming practices and tell me if there’s a better way to solve this if possible.
I have a grid. It pulls data from an Access database. (I’m using UCanAccess) I allow the user to filter the entries by Date, then make a selection, and choose to email those entries.
Let’s say I run the program. On the page, I choose a date, the Grid will pull the info from that date. I select a few entries, and click the sendToEmailBtn. Everything works.
Now I choose another date, and the grid is refreshed with a different set of entries. Everything displays properly, it pulled the data correctly.
But this time, when I click sendToEmailBtn, I get a nullpointerexception. I’m guessing because the indexID for the grid is now different from the Index of the BeanItemContainer. (since in the code, to change the grid, I simply used removeAllItems) But I’m not sure what to do here? Is there any other way to do this?I tried to just do this
I wanted to try setting the beanHolder = null and resetting up the grid, etc, but I thought there had to be a better way? Unless there isn’t? I just wanted to know if there’s any other way of using an object to get a bean from BeanItemContainer.
Setting up grid:
[code]
BeanContainer<String, Bean> beanHolder = new BeanContainer<String, Bean>(Bean.class);
beanHoldier.setBeanIdProperty("TID");
//Sets up the grid of Beans.
Grid grid = new Grid (beanHolder);
grid.setEditorEnabled(true); //Allow changing some parts of the Bean,
//might change later to not allow any changes.
grid.removeColumn("TID"); //Don't show this column, but it is there.
grid.setSelectionMode(SelectionMode.MULTI); //Allows to pick multiple entries.
[/code]
This is my date filter button:
dateButton.addClickListener(new ClickListener() {
private static final long serialVersionUID = 1L;
@Override
public void buttonClick(ClickEvent event){
beanHolder.removeAllItems();
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://\\\\server\\folder\\AccessDatabase.mdb");
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT [PLANT]
, [DEPARTMENT]
, [Entry-dt]
, [TID]
FROM [Query1]
where [RESOURCE]
NOT LIKE 'Example*' AND [Entry-dt]
= #"+new SimpleDateFormat("MM/dd/yyyy").format(df.getValue())+"#");
Bean bean = new Bean();
while (rs.next()) {
bean.setPlant(rs.getString(1));
bean.setDepartment(rs.getString(2));
bean.setEntryDate(rs.getString(3).split("\\s")[0]
.split("-")[1]
+ rs.getString(3).split("\\s")[0]
.split("-")[2]
+ rs.getString(3).split("\\s")[0]
.split("-")[0]
.substring(2));
beanHolder.addBean(bean);
bean = null;
bean = new Bean();
}
rs.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
});
And this is where the selection error happens (nullpointerexception)
sendToEmailBtn.addClickListener(new ClickListener() {
private static final long serialVersionUID = 1L;
@Override
public void buttonClick(ClickEvent event){
//*
A lot of email code
*//
try{
//Container to hold failed posted Beans
BeanContainer<String, Bean> failureBeans = new BeanContainer<String, Bean>(Bean.class);
//Email code cut out
//Gets which Time entries were selected by User.
MultiSelectionModel selection = (MultiSelectionModel) grid.getSelectionModel();
selection.getSelectedRows(); ////////////Error happens here
//Cycles through each one and emails
for (Object itemId: selection.getSelectedRows()){
beanHolder.getItem(itemId).getBean();
//tests for failed sent entries
if (Body.asText().contains("row 1")){
pw.write(beanHolder.getItem(itemId).getBean().toString()+ "\r\n");
failureBeans.addBean(beanHolder.getItem(itemId).getBean());
}
}//End For Loop
if (failureBeans.size() >0)
UI.getCurrent().addWindow(new BeanErrorWindow(failureBeans));
pw.close();
webClient.close();
}catch(FailingHttpStatusCodeException | IOException e){
e.printStackTrace();
}
Notification.show("Completed.");
} // Click Event
I changed some of the variable names to try to make the code easier to read.