0

Hello I've read some threads in here about using ajax to confirm a password in jsf. However while trying I'm getting a NPE when trying to get the string from the confirmed password. I don't know why, but it worked before I tried to render an icon saying the test failed. So I'm assuming that when I try to render the icons using ajax, since the confirm password field is null there goes the exception. However even with a test before assigning the value to the string confirmPassword the error still comes.

my form:

            <!-- password -->           

            <tr><td>
            <h:outputLabel for="password">#{registering.Password}</h:outputLabel></td><td>
            <p:password id="password" value="#{subscribeUser.user.password}" feedback="true"
                inline="true"
                promptLabel="#{registering.PleaseEnterPass}" weakLabel="#{registering.Weak}"
                goodLabel="#{registering.Good}" strongLabel="#{registering.Strong}"
                requiredMessage="#{registering.reqPassword}"
                validator="#{passwordValidator.validate}">
                <f:attribute name="confirm" value="#{confirmPassword}" />

<!-- This event leads to NPE -->
                <f:ajax event="blur" render="passwordCheck"></f:ajax>                   
            </p:password></td><td>

<!-- Here are the icons which "create" the NPE -->
            <h:panelGroup id="passwordCheck"  >
                <h:graphicImage library="images/icons" name="failed_indicator.png" rendered="#{passwordValidator.isIndicatorVisible.usernameFailed}"></h:graphicImage>
                <h:graphicImage library="images/icons" name="success_indicator.png" rendered="#{passwordValidator.isIndicatorVisible.usernameSuccess}"></h:graphicImage>
            </h:panelGroup></td><td>
            <span class="error"><h:message id="passwordMessage" for="password"/></span></td>
            </tr>           

            <!-- Confirm password -->

            <tr><td>
            <h:outputLabel for="confirmPassword" value="Confirm password : " /></td><td>
            <p:password id="confirmPassword" required="true"
                requiredMessage="#{registering.PleaseConfirmPassword}" 
                validator="#{passwordValidator.validate}"
                binding="#{confirmPassword}">
                <f:ajax event="blur" execute="password confirmPassword" render="passwordMessage passwordConfMessage"/>                      
                </p:password> </td><td></td><td>
            <span class="error"><h:message id="passwordConfMessage" for="confirmPassword"/></span></td>
            </tr>

        </table>
        <h:commandButton value="Submit" action="#{subscribeUser.inscrireUser}"></h:commandButton>

    </h:form>

my validator bean:

  @ManagedBean
public class PasswordValidator implements Validator {

    private Map<String, Boolean> isIndicatorVisible;

    public PasswordValidator() {
        this.isIndicatorVisible = new HashMap<>();
        this.isIndicatorVisible.put("passwordSuccess", false);
        this.isIndicatorVisible.put("passwordFailed", false);
    }

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

        String password = value.toString();
        UIInput uiInputConfirmPassword = (UIInput) component.getAttributes()
                .get("confirm");
        String confirmPassword = uiInputConfirmPassword.getSubmittedValue()
                .toString(); //this line error

THE ERROR IS ABOVE THIS LINE


        if (password == null || password.isEmpty() || confirmPassword == null
                || confirmPassword.isEmpty()) {
            this.isIndicatorVisible.replace("passwordFailed", false);
            this.isIndicatorVisible.replace("passwordSuccess", true);
            return;
        }
        if (password.length() < 6) {
            uiInputConfirmPassword.setValid(false);
            this.isIndicatorVisible.replace("passwordSuccess", false);
            this.isIndicatorVisible.replace("passwordFailed", true);
            throw new ValidatorException(new FacesMessage(
                    "Your password must be at least 6 characters long."));
        }
        if (!password.equals(confirmPassword)) {
            uiInputConfirmPassword.setValid(false);
            this.isIndicatorVisible.replace("passwordSuccess", false);
            this.isIndicatorVisible.replace("passwordFailed", true);
            throw new ValidatorException(new FacesMessage(
                    "Passwords don't match."));
        }
    }

    public Map<String, Boolean> getIsIndicatorVisible() {
        return isIndicatorVisible;
    }
}

Those test give me the values 2a and 2b.

    if (component.getAttributes().get("confirm") == null) {
        System.out.println("1a");
    } else {
        System.out.println("2a");
    }

    if (((UIInput) component.getAttributes().get("confirm"))
            .getSubmittedValue().toString() == null) {
        System.out.println("1b");
    } else {
        System.out.println("2b");
    }

However the second test "b" gives me an NPE.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ced
  • 15,847
  • 14
  • 87
  • 146
  • An NPE comes with a stacktrace. You can and should analyze that yourself. It contains info on where it comes from. And no, you cannot ask a second question. For a very simple reason. Answering and comments can and will get mixed up and hard to flow. Now and in the future when others read it. – Kukeltje May 15 '15 at 20:38
  • If you know what an NPE is you know the stacktrace is relevant. You stilldid not post it – Kukeltje May 15 '15 at 20:44
  • I edited my post it wasn't written clearly. – Ced May 15 '15 at 21:28
  • Still no stacktrace. Which line _exactly_ is throwing the exception? – DavidS May 15 '15 at 21:31
  • 1
    the line marked with a comment. I know it's not obvious but I didn't know how else I could show it. There you go I split the code so it's not missable now. Hopefully. – Ced May 15 '15 at 21:35
  • Good! Have you confirmed which object is null? Is it `uiInputConfirmPassword` or is it `getSubmittedValue`? – DavidS May 15 '15 at 21:51
  • Hum I don't know. I did some test as edited but neither seem to be null. – Ced May 15 '15 at 22:19
  • You forgot to execute the confirm input. Consequence is rather obvious. – BalusC May 15 '15 at 22:22
  • Thanks. It was indeed the problem. It was part of my code that was copy pasted from one of your other comments actually. And I didn't search to understand it. I know it's really bad practice but sometimes when things don't go the way I expect them to go just get frustrated (well especially if it goes on and on like I had here, everything I tried seemed to bring a new set of issue). In my defense it can be frustrating at times. Thanks for your input. – Ced May 20 '15 at 23:52

0 Answers0