I am trying to use the java regex matcher-class and I'm encountering a NullPointerException in my code:
String input = null;
System.out.println("\nEnter your username: ");
Matcher matcher = VALID_USERNAME_REGEX.matcher(input); //EXCEPTION IS HERE
if (matcher.find()) {
username = input;
} else {
input = null;
System.out.println("\nInvalid input, please try again!");
}
Stack Trace: (line: 173 is in the above code where the comment "//EXCEPTION IS HERE
" is located)
Exception in thread "main" java.lang.NullPointerException
at java.util.regex.Matcher.getTextLength(Unknown Source)
at java.util.regex.Matcher.reset(Unknown Source)
at java.util.regex.Matcher.<init>(Unknown Source)
at java.util.regex.Pattern.matcher(Unknown Source)
at org.console.Interface.createAccount(Interface.java:173)
at org.console.Interface.login(Interface.java:78)
at org.application.Application.run(Application.java:31)
at org.application.Application.main(Application.java:37)
The value of the input should be what the user enters in the console. The regex should match the input to validate that it meets the regex requirements.