【问题标题】:Why on the same variable <p:selectBooleanCheckbox/> throws NPE while <h:selectBooleanCheckbox/> doesn't?为什么在同一个变量上 <p:selectBooleanCheckbox/> 会抛出 NPE 而 <h:selectBooleanCheckbox/> 不会?
【发布时间】:2014-02-25 11:02:59
【问题描述】:

我有下一个代码 sn-p 可以与 &lt;h:selectBooleanCheckbox/&gt; 一起按预期工作,如果我使用 &lt;p:selectBooleanCheckbox/&gt; 则会失败:

<h:form id="dlg6form">
    <h:panelGrid id="panel6" columns="3">
        <h:outputLabel for="appName2" value="Name: "/>
        <p:inputText id="appName2" required="true" value="#{homeBean.selectedApplication.appName}" 
                     label="Name">
            <f:validator binding="#{uniqueApplicationValidator}"/>
        </p:inputText>
        <p:message for="appName2"/>

        <h:outputLabel for="vendorName2" value="Vendor: "/>
        <p:inputText id="vendorName2" value="#{homeBean.selectedApplication.vendorName}"
                     label="Vendor" required="true" />
        <p:message for="vendorName2"/>

        <h:outputLabel for="appLicense2" value="Requires license: "/>
        <p:selectBooleanCheckbox id="appLicense2" value="#{homeBean.selectedApplication.licenseRequired}" style="height:22px;width:22px;margin: 0px;" />
        <p:message for="appLicense2"/>


    </h:panelGrid>

    <p:commandButton type="button" value="Cancel" onclick="dlg6.hide()" />

    <p:commandButton value="OK" process="@form" update=":dlg6form:panel6, :tab:applications" action="#{homeBean.editApplication}"
                     oncomplete="if (args &amp;&amp; !args.validationFailed) dlg6.hide()"/>
</h:form>

这是输出:

/pages/home.xhtml @424,149 value="#{homeBean.selectedApplication.licenseRequired}": Target Unreachable, 'selectedApplication' returned null

变量、它们的值、方法等都可以,所以我想没有必要提供它的代码,但如果你愿意,你可以查看我之前的问题以获取更多详细信息以及所有必要的代码。 PrimeFaces can't get boolean value from dataTable's selected row

所以,问题是:为什么在同一个变量上&lt;p:selectBooleanCheckbox/&gt; 会抛出 NPE 而&lt;h:selectBooleanCheckbox/&gt; 不会?如何解决这个问题?我想使用 PrimeFaces 组件。

谢谢!

//已编辑

HomeBean.java:

import com.infostroy.adminportal.bean.BaseBean;
import com.infostroy.adminportal.model.Application;
import com.infostroy.adminportal.model.Computer;
import com.infostroy.adminportal.model.User;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.annotation.PostConstruct;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("session")
public class HomeBean extends BaseBean {

    private static final String editUserBtn = "tab:form1:editUser";
    private static final String deleteUserBtn = "tab:form1:deleteUser";
    private static final String editCompBtn = "tab:form2:editComp";
    private static final String deleteCompBtn = "tab:form2:deleteComp";
    private static final String editAppBtn = "tab:form3:editApp";
    private static final String deleteAppBtn = "tab:form3:deleteApp";

    private List<User> users;
    private List<Computer> computers;
    private List<Application> applications;
    private User selectedUser, newUser;
    private Computer selectedComputer, newComputer;
    private Application selectedApplication, newApplication;
    private String deleteUserMsg, deleteCompMsg, deleteAppMsg;
    private RequestContext rc;

    @PostConstruct
    public void init() {
        setUsers(hibernateDBManager.getAllUsers());
        setComputers(hibernateDBManager.getAllComputers());
        setApplications(hibernateDBManager.getAllApplications());
        newUser = new User();
        newComputer = new Computer();
        newApplication = new Application();
        rc = RequestContext.getCurrentInstance();
    }

    public void addUser() throws NoSuchAlgorithmException {
        if (hibernateDBManager.insertUser(newUser)) {
            users.add(newUser);
            newUser = new User();
            updateUserButtons();
        }
    }

    public void editUser() throws NoSuchAlgorithmException {
        if (hibernateDBManager.updateUser(selectedUser)) {
            users.set(users.indexOf(selectedUser), selectedUser);
            selectedUser = null;
            updateUserButtons();
        }
    }

    public void deleteUser() throws IOException {
        if (selectedUser != null) {
            if (hibernateDBManager.deleteUserById(selectedUser.getUserId()) > 0) {
                users.remove(selectedUser);
                selectedUser = null;
                updateUserButtons();
            }
        }
    }

    public void addComputer() {
        if (newComputer != null && hibernateDBManager.insertComputer(newComputer)) {
            computers.add(newComputer);
            newComputer = new Computer();
            updateCompButtons();
        }
    }

    public void editComputer() {
        if (hibernateDBManager.updateComputer(selectedComputer)) {
            computers.set(computers.indexOf(selectedComputer), selectedComputer);
            selectedComputer = null;
            updateCompButtons();
        }
    }

    public void deleteComputer() {
        if (selectedComputer != null) {
            if (hibernateDBManager.deleteComputerById(selectedComputer.getComputerId()) > 0) {
                computers.remove(selectedComputer);
                selectedComputer = null;
                updateCompButtons();
            }
        }
    }

    public void addApplication() {
        if (newApplication != null && hibernateDBManager.insertApplication(newApplication)) {
            applications.add(newApplication);
            newApplication = new Application();
            updateAppButtons();
        }
    }

    public void editApplication() {
        if (hibernateDBManager.updateApplication(selectedApplication)) {
            applications.set(applications.indexOf(selectedApplication), selectedApplication);
            selectedApplication = null;
            updateAppButtons();
        } 
    }

    public void deleteApplication() {
        if (selectedApplication != null) {
            if (hibernateDBManager.deleteApplicationById(selectedApplication.getAppId()) > 0) {
                applications.remove(selectedApplication);
                selectedApplication = null;
                updateAppButtons();
            }
        }
    }

    public void onUserRowSelect(SelectEvent event) {
        setSelectedUser((User) event.getObject());
        setDeleteUserMsg("Are you sure you want to delete user "
                + selectedUser.getFirstName() + " " + selectedUser.getLastName() + "?");
    }

    public void onCompRowSelect(SelectEvent event) {
        setSelectedComputer((Computer) event.getObject());
        deleteCompMsg = "Are you sure you want to delete computer "
                + selectedComputer.getComputerName()
                + " (" + selectedComputer.getIpAddress() + ") ?";
    }

    public void onAppRowSelect(SelectEvent event) {
        setSelectedApplication((Application) event.getObject());
        deleteAppMsg = "Are you sure you want to delete application "
                + selectedApplication.getAppName() + "?";
    }

    protected void updateUserButtons() {
        rc.update(editUserBtn);
        rc.update(deleteUserBtn);
    }

    protected void updateCompButtons() {
        rc.update(editCompBtn);
        rc.update(deleteCompBtn);
    }

    protected void updateAppButtons() {
        rc.update(editAppBtn);
        rc.update(deleteAppBtn);
    }

    public String getDeleteUserMsg() {
        return deleteUserMsg;
    }

    public void setDeleteUserMsg(String deleteUserMsg) {
        this.deleteUserMsg = deleteUserMsg;
    }

    public String getDeleteCompMsg() {
        return deleteCompMsg;
    }

    public void setDeleteCompMsg(String deleteCompMsg) {
        this.deleteCompMsg = deleteCompMsg;
    }

    public String getDeleteAppMsg() {
        return deleteAppMsg;
    }

    public void setDeleteAppMsg(String deleteAppMsg) {
        this.deleteAppMsg = deleteAppMsg;
    }
    //Other getters/setters

}

【问题讨论】:

  • 请发布您的托管 bean 代码。
  • 添加 HomeBean 类代码。
  • 能否也粘贴异常?

标签: jsf jsf-2 primefaces nullpointerexception components


【解决方案1】:
    /pages/home.xhtml @424,149 
value="#{homeBean.selectedApplication.licenseRequired}": 
Target Unreachable, 'selectedApplication' returned null

这表明selectedApplication 对象为空。修复它,然后你的 primefaces booleancheckbox 应该可以工作了。

【讨论】:

  • 不,它不为空。只是为什么它会在 上停止?它之前有两行代码,它们使用相同的变量并且不会导致 null。
  • 如果我删除这行使用 的代码,则不会抛出 NPE。
  • OK 发布有这个变量的类。
  • 查看此链接,所有内容都在此处进行了描述:stackoverflow.com/questions/21498973/…
猜你喜欢
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 2013-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多