3

当我在邮政编码中输入内容并点击提交并将国家/地区设置为默认空值时,我得到 javax.faces.FacesException: java.lang.NullPointerException。如果我选择国家,然后输入一切正常的东西。我尝试了 SubmittedValue,但它的工作方式相反 - null 正在工作,之后给出 null 异常。

@FacesValidator("zipV")
public class ZipValidator implements Validator {

LocaleBean Bean = new LocaleBean();
String language;
private static final String ZIP_PATTERN_BG = "\\d{4}";
private static final String ZIP_PATTERN_US = "\\d{5}";
private static final String ZIP_PATTERN_DEFAULT = "[A-Za-z0-9]*";
private String zip_pattern;
private Pattern pattern;
private Matcher matcher;
private String country;


@Override
public void validate(FacesContext context, UIComponent component, Object value) throws       ValidatorException {

    language = Bean.getLanguage();

    UIInput Input = (UIInput) component.getAttributes().get("country");
    country = Input.getValue().toString();
    String zip = (String) value;


    if (country == null || country.isEmpty()) {
        return; 
    }

    switch (country) {
        case "BGR":
            zip_pattern = ZIP_PATTERN_BG;
            break;
        case "USA":
            zip_pattern = ZIP_PATTERN_US;
            break;
        default:
            zip_pattern = ZIP_PATTERN_DEFAULT;
            break;
    }

    pattern = Pattern.compile(zip_pattern);

    matcher = pattern.matcher(value.toString());
    if (!matcher.matches()) {


        switch (language) {
            case "en": {
                FacesMessage msg = new FacesMessage("Invalid zip.");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(msg);
            }
            case "bg": {
                FacesMessage msg = new FacesMessage("Невалиден пощенски код.");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(msg);
            }
        }
    }
}
}

这是视图:

<h:selectOneMenu id="country" value="#{account.country}" required="true" requiredMessage="#{msg['register.required']}" binding="#{country}">
<f:selectItem itemValue="#{null}" itemLabel="#{msg['register.countryQ']}"/>
<f:selectItems value="#{account.countries}"/>
<f:ajax listener="#{account.loadStates()}" render="state"/>
</h:selectOneMenu>

<h:inputText id="zipcode" required="true" requiredMessage="#{msg['register.required']}" value="#{account.zipcode}">
<f:validator validatorId="zipV"/>
<f:attribute name="country" value="#{country}"/>
</h:inputText>
4

1 回答 1

2

这里,

country = Input.getValue().toString();

你根本不应该使用toString()。你应该铸造它:

country = (String) Input.getValue();

否则返回NullPointerException时会抛出. 正如它的 javadoc明确指出的那样,当您尝试调用实例方法时(就像您对 所做的那样),将抛出a 。getValue()nullNullPointerExceptionnulltoString()

请注意,这个问题在技术上与 JSF 无关。这只是基本的Java。异常的java.lang包在这方面是一个很好的提示。javax.faces如果你遇到了(或)包的异常javax.el,那么我们可以讨论一个真正的 JSF(或 EL)问题。

也可以看看:


与具体问题无关,我真的很尊重Java 命名约定。变量名以小写字母开头。使用input而不是Input. 此外,您对本地化的手动控制很奇怪。如果您需要支持 10 种语言怎么办?你把开关扩展到所有地方吗?<resource-bundle>使用带有和的 JSF 内置本地化工具ResourceBundle#getBundle()

于 2013-09-06T17:16:39.747 回答