egslava/edittext-mask

Not possible to set mask in runtime only

mikhailmelnik opened this issue · 4 comments

When mask is omitted in xml layout to be set later in runtime application fails with the following exception:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
    at br.com.sapereaude.maskedEditText.MaskedEditText.generatePositionArrays(MaskedEditText.java:205)
    at br.com.sapereaude.maskedEditText.MaskedEditText.cleanUp(MaskedEditText.java:122)
    at br.com.sapereaude.maskedEditText.MaskedEditText.<init>(MaskedEditText.java:69)

The workaround to this is to:
Set any mask in xml and then change it on runtime.
Make sure you are setting hint and hint is not empty string.

@amrikd yep, I should have probably mention it.

I faced this issue when extending the class.

I fixed it by adding:

if(mask == null || mask.length() <= 1){
    mask = Character.toString(charRepresentation);
}

Into the MaskedEditText's constructor, after the "charRepresentation" if/else. This way, by default, it has a single digit input/clear mask.

But, at least I can set the mask and representation when overriding the constructor.

@vitorhugods the pull-request is welcome! :) I know, you can do all the best pull-requests ever :) Or I'll elaborate on it later, when I finish classification of all the other issues.