0
votes

I want to verify that the social security number exists in the database. This is my Validator.

@RequestScoped
@FacesValidator("personalIdValidator")
public class PersonalIdValidator implements Validator {

    @EJB
    private UserDaoLocal userDao;

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

        boolean ok;
        String personalId = value.toString();
        String regex = "[0-9]{11}";
        ok = Pattern.matches(regex, personalId);

        if (!ok || userDao.findUserExist(personalId)) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Błędny pesel", "Błędny pesel"));
        }
    }
}

In Validator i have a @EJB userDao and I do a method userDao.findUserExist(personalId) This method:

@Override
public boolean findUserExist(String personalId) {
    try {
        Query q = em.createQuery("SELECT u FROM User u WHERE u.personalId = :personalId");
        q.setParameter("personalId", personalId);
        user = (User) q.getSingleResult();
        if(user == null) {
            return false;
        } else {
            return true;
        }

    } catch (Exception e) {
        System.out.println("Błąd" + e.getMessage());
        return false;
    }
}

But when I do this method I have a error:

WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception java.lang.NullPointerException at pl.ePrzychodnia.validators.PersonalIdValidator.validate(PersonalIdValidator.java:41) at javax.faces.component.UIInput.validateValue(UIInput.java:1149) at javax.faces.component.UIInput.validate(UIInput.java:967) at javax.faces.component.UIInput.executeValidate(UIInput.java:1233) at javax.faces.component.UIInput.processValidators(UIInput.java:698) at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) at org.primefaces.component.panel.Panel.processValidators(Panel.java:297) at javax.faces.component.UIForm.processValidators(UIForm.java:253) at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214) at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172) at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) at com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:722)

1
You obviously get a null pointer exception in validate(). But which line is it? Have you tried debugging it?Magnilex

1 Answers

0
votes

First one :

Replace

String personalId = value.toString();

By

String confirm = (String) component.getAttributes().get("theInputFromYourPage");

You can also try this :

private static final String regex = "[0-9]{11}";
private Pattern pattern;
private Matcher matcher;

public PersonalIdValidator() {
    pattern = Pattern.compile(regex);
}

public void validate(FacesContext context, UIComponent component,
        Object value) throws ValidatorException {
    // TODO Auto-generated method stub
    matcher = pattern.matcher(value.toString());
    if (!matcher.matches()) {


        // DO YOUR STUFF QUERY


    } else {
                    // FacesMessages, invite the user to respect regex [0-9]{11}
            }
}