Java Regex Matcher 在处理空输入时遇到 NullPointerException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41286632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 05:47:29  来源:igfitidea点击:

Java Regex Matcher encountering NullPointerException, when handed null input

javanullpointerexception

提问by Brenton

I am trying to use the java regex matcher-class and I'm encountering a NullPointerException in my code:

我正在尝试使用 java regex 匹配器类,但在我的代码中遇到 NullPointerException:

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)

堆栈跟踪:(第173行在上面的代码中注释“ //EXCEPTION IS HERE”所在的位置)

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.

输入的值应该是用户在控制台中输入的内容。正则表达式应与输入匹配以验证它是否满足正则表达式要求。

回答by Marvin

It's not explicitly stated in the javadoc of Pattern#matcher, but it will not work with a nullinput (it creates a new Matcherinstance which will reset itself upon initialization, leading to the observed exception). You'll have to explicitly test for != nullbeforehand.

它在 的 javadoc 中没有明确说明Pattern#matcher,但它不适用于null输入(它创建一个新Matcher实例,该实例将在初始化时自行重置,从而导致观察到的异常)。您必须!= null事先明确测试。

Side note: Although undocumented, this has already been declined to change:

旁注:虽然没有记录,但这已经被拒绝改变

The current API doc does not explicitly or implicitly mention whether or not a NPE will/should be thrown. But it is difficult to define a good semantics for a "null" in Matcher as a legal input text, should it be semantically equivalent to empty string ""? Obviously not. Then, what the match/find result should be expected when match null against boundary pattern like "^" and "$" (probably no match/no find for all patterns), and who will be benefited from such a semantics? The described use scenario is reasonable but should be easily achieved by use an empty string "" instead. Dont see a strong justification to "fix" the current behavior. Instead, updating the package spec to describe the NPE behavior might be a better choice.

当前的 API 文档没有明确或隐含地提及是否会/应该抛出 NPE。但是对于Matcher中的“null”作为合法的输入文本很难定义一个好的语义,是否应该在语义上等同于空字符串“”?明显不是。那么,当将 null 与边界模式(可能所有模式都没有匹配/未找到)匹配 null 时,应该期望匹配/查找结果是什么,谁将从这样的语义中受益?所描述的使用场景是合理的,但应该通过使用空字符串 "" 来轻松实现。不要看到“修复”当前行为的有力理由。相反,更新包规范以描述 NPE 行为可能是更好的选择。

回答by Brenton

I added input = in.nextLine();above the matcher line.

input = in.nextLine();在匹配器行上方添加了。

String input = null;
System.out.println("\nEnter your username: ");
input = in.nextLine();
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!");
}