通过控制器类更新列表框时出现空指针异常

时间:2018-06-20 11:53:41

标签: zk zul

我正在从控制器类加载Listbox模板。这是一个假期余额表格。我想更新员工的假期。之前一切正常。最近,我引入了名为“ Leave Adjusted”的额外列。添加完之后,每当我单击“更新”按钮时,都会抛出“空指针异常”错误图像:
Image。我已经尝试了所有可能的解决方案,但我做不到。

我的Zul代码: leaveBalanceMaster.zul

<?page title="Leave Types" contentType="text/html;charset=UTF-8"?>

<zk xmlns:w='client'>
    <style src="/resources/zul/css/style.css" />
    <hbox>
        <div sclass="breadcrumb">
            <a href="/dashboard" iconSclass="fa fa-home" id="home"
                label="Home" />
            /
            <a sclass="active" label="Leave Balance Master" />
        </div>
    </hbox>
    <separator height="10%"></separator>
    <window width="100%" id="leavetype" border="normal"
        sclass="column1">
        <textbox id="searchTextBox"
            style="float:left;margin-bottom:1%">
        </textbox>
        <button style="float:left" id="searchIcon"
            iconSclass="fa fa-search">
        </button>

        <div align="right" style="padding-right: 3%;">

            <button label="Update" zclass="btn btn-success btn-sm "
                id="updateLeaveBalance" />
        </div>

        <separator spacing="7px"></separator>
        <div
            apply="com.madrone.hrms.zul.controller.LeaveBalanceMasterController">
            <div id="alertcustom"></div>
                <listbox width="auto" style="margin-top: 1%; border-top:none" mold="paging"
                pageSize="10" emptyMessage="No Employee Found in the list"
                height="80%" id="listleaveBalances">
                <auxhead>
                    <auxheader id="au1" style="border-top:0.5px solid #cfcfcf"></auxheader>
                    <auxheader id="au2" label="Opening Balance" zclass="border border-top"></auxheader>
                    <auxheader id="au3" label="Leave Taken" zclass="border border-top"></auxheader>
                    <auxheader id="au4" label="Closing Balance" zclass=" border-right border-top"></auxheader>
                    <auxheader id="au5" label="Leave Adjusted" zclass=" border-right border-top"></auxheader>
                </auxhead>
                <listhead></listhead>

                <template name="model"></template>
            </listbox>
        </div>
    </window>
</zk>

我的控制器类: LeaveBalanceMasterController.java

public class LeaveBalanceMasterController extends SelectorComposer<Component> {

    private static final long serialVersionUID = -2015612522126627089L;
    private static Logger LOG = Logger.getLogger(LeaveBalanceMasterController.class.getName());

    public static ServiceHelper helper;

    private Session session = Sessions.getCurrent();

    @Wire
    Listbox listleaveBalances;

    @Wire
    Button updateLeaveBalance;

    @Wire
    Textbox searchTextBox;

    @Wire
    Button searchIcon;

    @Wire
    Auxheader au1;

    @Wire
    Auxheader au2;

    @Wire
    Auxheader au3;

    @Wire
    Auxheader au4;

    @Wire
    Auxheader au5;

    @Wire
    Window leaveAdjustModal;

    List<Employee> empList = null;

    List<LeaveTypes> ltList;

    @Override
    public void doAfterCompose(Component comp) {
        try {
            super.doAfterCompose(comp);

            AddLeaveBalanceList();
            addEventListeners();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOG.info(e);
        }
    }

    private void addEventListeners() {
        searchTextBox.addEventListener(Events.ON_OK, event -> setFilteredList());
        searchTextBox.addEventListener(Events.ON_BLUR, event -> setFilteredList());
        searchIcon.addEventListener(Events.ON_OK, event -> setFilteredList());
        searchIcon.addEventListener(Events.ON_CLICK, event -> setFilteredList());

    }

    private void setFilteredList() throws DaoException {
        List<Employee> filteredEmpList = new ArrayList<>();
        if (null != searchTextBox.getValue()) {
            for (Employee empItr : empList) {
                if ((empItr.getCode().toLowerCase().trim().contains(searchTextBox.getValue().toLowerCase().trim()))
                        || (empItr.getFullName().toLowerCase().contains(searchTextBox.getValue().toLowerCase().trim()))
                        || empItr.getDepartment().getName().toLowerCase()
                                .contains(searchTextBox.getValue().toLowerCase().trim())
                        || (empItr.getDesignation().getDesignation_name().toLowerCase().trim()
                                .contains(searchTextBox.getValue().toLowerCase().trim()))) {
                    filteredEmpList.add(empItr);
                }
            }
        }
        Collections.sort(filteredEmpList, (e1, e2) -> e1.getCode().compareTo(e2.getCode()));
        setListBox(filteredEmpList, ltList.size());
    }

    private void setListBox(List<Employee> empList, int j) throws DaoException {
        if (CommonCheckUtil.checkObjectNotNull(empList)) {
            ListModel<Employee> empListModel = new ListModelList<Employee>(empList);
            List<LeaveBalance> leaveBalanceList = helper.getMiLmsService().listLeaveBalance();
            listleaveBalances.setItemRenderer((item, data, arg2) -> {
                Employee emp = (Employee) data;
                item.appendChild(new Listcell(emp.getCode()));

                item.setAttribute("employee", emp);
                item.appendChild(new Listcell(emp.getFullName()));
                Listhead lhList = listleaveBalances.getListhead();
                for (int i = 0; i < j; i++) {
                    Doublebox dBox = new Doublebox();
                    dBox.setFormat("####.#");
                    dBox.setStyle("width: 75%;");
                    for (LeaveBalance lbl : leaveBalanceList) {

                        if (lbl.getEmployee().getGkey().equals(emp.getGkey())
                                && ((LeaveTypes) lhList.getChildren().get(2 + i).getAttribute("leaveTypes")).getGkey()
                                        .equals(lbl.getLeaveTypes().getGkey())) {

                            dBox.setValue(Double.parseDouble(lbl.getOpeningBalance()));
                            dBox.setAttribute("gkey", lbl.getGkey());

                        }
                    }
                    dBox.addEventListener(Events.ON_BLUR,
                            event -> UpperBoundValidation(((Doublebox) event.getTarget()).getValue(),
                                    event.getTarget()));
                    Listcell lc = new Listcell();
                    lc.appendChild(dBox);
                    item.appendChild(lc);
                    if (lc.getColumnIndex() == 2) {
                        lc.setStyle("border-left:5px double #cfcfcf ");

                    }
                }
                for (int i = 0; i < j; i++) {
                    Doublebox dBox = new Doublebox();
                    dBox.setFormat("####.#");
                    dBox.setStyle("width: 75%;background: #ffffff !important;color: #000!important;");
                    dBox.setDisabled(Boolean.TRUE);
                    // dBox.addEventListener(Events.ON_CTRL_KEY, event -> {
                    // KeyEvent ke = (KeyEvent) event;
                    // System.out.println(ke.getKeyCode());
                    // });
                    for (LeaveBalance lbl : leaveBalanceList) {

                        if (lbl.getEmployee().getGkey().equals(emp.getGkey())
                                && ((LeaveTypes) lhList.getChildren().get(2 + i).getAttribute("leaveTypes")).getGkey()
                                        .equals(lbl.getLeaveTypes().getGkey())) {

                            dBox.setValue(Double.parseDouble(lbl.getOpeningBalance())
                                    - Double.parseDouble(lbl.getClosingBalance()));
                            dBox.setAttribute("gkey", lbl.getGkey());

                        }
                    }
                    dBox.addEventListener(Events.ON_BLUR,
                            event -> UpperBoundValidation(((Doublebox) event.getTarget()).getValue(),
                                    event.getTarget()));
                    Listcell lc = new Listcell();
                    lc.appendChild(dBox);
                    item.appendChild(lc);
                    if (lc.getColumnIndex() == 6) {
                        lc.setStyle("border-left:5px double #cfcfcf ");

                    }
                }
                for (int i = 0; i < j; i++) {
                    Doublebox dBox = new Doublebox();
                    dBox.setFormat("####.#");
                    dBox.setStyle("width: 75%;background: #ffffff !important;color: #000!important;");
                    dBox.setDisabled(Boolean.TRUE);
                    // dBox.addEventListener(Events.ON_CTRL_KEY, event -> {
                    // KeyEvent ke = (KeyEvent) event;
                    // System.out.println(ke.getKeyCode());
                    // });
                    for (LeaveBalance lbl : leaveBalanceList) {

                        if (lbl.getEmployee().getGkey().equals(emp.getGkey())
                                && ((LeaveTypes) lhList.getChildren().get(2 + i).getAttribute("leaveTypes")).getGkey()
                                        .equals(lbl.getLeaveTypes().getGkey())) {

                            dBox.setValue(Double.parseDouble(lbl.getClosingBalance()));
                            dBox.setAttribute("gkey", lbl.getGkey());

                        }
                    }
                    dBox.addEventListener(Events.ON_BLUR,
                            event -> UpperBoundValidation(((Doublebox) event.getTarget()).getValue(),
                                    event.getTarget()));
                    Listcell lc = new Listcell();
                    lc.appendChild(dBox);
                    item.appendChild(lc);
                    if (lc.getColumnIndex() == 10) {
                        lc.setStyle("border-left:5px double #cfcfcf ");

                    } else if (lc.getColumnIndex() == 13) {
                        lc.setStyle("border-right:5px double #cfcfcf ");

                    }
                }
                Doublebox dBox = new Doublebox();
                dBox.setFormat("####.#");
                dBox.setStyle("width: 30%;background: #ffffff !important;color: #000!important;margin-left:30%;text-align:center;");
                for (LeaveBalance lbl : leaveBalanceList) {
                dBox.setValue(Double.parseDouble(lbl.getLeaveAdjusted()));
                dBox.setAttribute("gkey", lbl.getGkey());
                }
                Listcell lcs = new Listcell();
                lcs.appendChild(dBox);
                item.appendChild(lcs);
            });
            listleaveBalances.setModel(empListModel);
        }
    }

    private Object UpperBoundValidation(Double data, Component box) {
        double maxValue = 100.0;

        if (data != null && data > maxValue) {
            throw new WrongValueException(box, "Cannot be greater than 100");
        } else {
            return data;
        }
    }

    private void AddLeaveBalanceList() {
        try {

            Listhead lh = listleaveBalances.getListhead();
            Listheader lhEmpId = new Listheader();
            lhEmpId.setLabel("Emp Id");
            lhEmpId.setSort("auto(code)");
            lhEmpId.setWidth("6%");
            lh.appendChild(lhEmpId);

            Listheader lhEmpName = new Listheader();
            lhEmpName.setLabel("Emp Name");
            lhEmpName.setSort("auto(fullName)");
            lhEmpName.setWidth("20%");
            lh.appendChild(lhEmpName);

            ltList = helper.getMiLmsService().listLeaveTypes();
            SortListUtil.sortListWithCustomCompare(ltList,
                    (o1, o2) -> ((LeaveTypes) o1).getGkey().compareTo(((LeaveTypes) o2).getGkey()));
            for (int i = 0; i < 3; i++) {
                for (LeaveTypes lt : ltList) {
                    Listheader lhl = new Listheader();
                    lhl.setLabel(lt.getLeaveCode());
                    lhl.setAttribute("leaveTypes", lt);
                    lhl.setWidth("5%");
                    lh.appendChild(lhl);

                    if (lhl.getColumnIndex() == 2 || lhl.getColumnIndex() == 6 || lhl.getColumnIndex() == 10) {
                        lhl.setStyle("border-left:5px double #cfcfcf ");
                    } else if (lhl.getColumnIndex() == 13) {

                        lhl.setStyle("border-right:5px double #cfcfcf ");
                    }
                }
            }

            Listheader lhAdj = new Listheader();
            lhAdj.setLabel("PL");
            lhAdj.setSort("auto(fullName)");
            lhAdj.setWidth("10%");
            lh.appendChild(lhAdj);

            au1.setColspan(2);

            au2.setColspan(ltList.size());
            au3.setColspan(ltList.size());
            au4.setColspan(ltList.size());
            au5.setColspan(1);
            setListItems(ltList.size());

        } catch (DaoException e) {
            // TODO Auto-generated catch block
            LOG.info(e);
        }

    }

    private void setListItems(int j) throws DaoException {
        empList = helper.getMiOrgService().listEmployee();
        Collections.sort(empList, (e1, e2) -> e1.getCode().compareTo(e2.getCode()));
        setListBox(empList, j);

    }

    @Listen("onClick=#updateLeaveBalance")
    public void updateLeaveBalance() {

        List<Listitem> liList = listleaveBalances.getItems();
        for (Listitem li : liList) {
            try {
                LeaveTypes lt = new LeaveTypes();
                LeaveBalance lb = new LeaveBalance();
                Double value = 0.0;
                List<Component> chList = li.getChildren();
                for (Component c : chList) {
                    if (c.getFirstChild() instanceof Doublebox) {
                        if (null != ((Doublebox) c.getFirstChild()).getValue()) {
                            Long gkey = (Long) ((Doublebox) c.getFirstChild()).getAttribute("gkey");
                            value = ((Doublebox) c.getFirstChild()).getValue();
                            lt = (LeaveTypes) ((Listcell) c).getListheader().getAttribute("leaveTypes");
                            lb.setLeaveTypes(lt);
                            lb.setOpeningBalance(String.valueOf(value));
                            lb.setClosingBalance(String.valueOf(value));
                            lb.setLeaveTaken("0");
                            lb.setDeleteFlag(Boolean.FALSE);
                            lb.setCurrentBalanceFlag(Boolean.TRUE);
                            lb.setEmployee((Employee) li.getAttribute("employee"));

                            if (null != gkey) {
                                lb.setLeaveAdjusted("10");
                                lb.setGkey(gkey);
                                helper.getMiLmsService().updateLeaveBalance(lb);
                            } else {
                                helper.getMiLmsService().addLeaveBalance(lb);
                            }
                        }
                    }

                }

            } catch (DaoException e) {
                // TODO Auto-generated catch block
                LOG.info(e);
            }
            // lb.setMonthYear(DateAndTimeUtil.getMonthYearName(Calendar.mo));

        }
        Executions.sendRedirect("/leaveBalanceList");
    }

}

0 个答案:

没有答案