小编Bre*_*ton的帖子

Java Regex Matcher在传递null输入时遇到NullPointerException

我试图使用java正则表达式匹配器类,我在我的代码中遇到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!");
}
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:(行: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)
Run Code Online (Sandbox Code Playgroud)

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

java nullpointerexception

5
推荐指数
1
解决办法
9213
查看次数

在JavaScript中将符号添加到整数的某个索引

我的目标是创建一个能够生成随机12位数的函数,然后使用具有特定格式的警报将此数字显示给用户.

我的最终结果将显示一个整数,其数字格式如下

1234-5678-4321
Run Code Online (Sandbox Code Playgroud)

目前我所拥有的是随机数生成器功能,您可以在下面看到.我不认为这是生成12位数字的最佳方法,但似乎有效.

function ticketGenerator() {
    return Math.floor((Math.random() * 899999999999) + 100000000000);
}
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚怎么做,就是"-"在整数索引中为每第4个数字添加符号.我想将此变量保存为数据库的Integer,因此我试图避免将此Integer解析为String但不知道是否可行.

javascript

0
推荐指数
1
解决办法
75
查看次数

标签 统计

java ×1

javascript ×1

nullpointerexception ×1