吉斯。(Guice. Inject into constructor)
我有一个单身人士:
public class MySingleton{ public static getInstance(){//typical singleton getInstance ... } //fields private static volatile instance; @Inject private AnotherClassInstanceThatIWantToInjectHere anotherClassInst_BlaBla; private MySingleton(){ ... anotherClassInst_BlaBla.doSmth();//NullPointerException happens! ... } }
这个NPE的原因是什么? 是否因为它是构造函数或因为它是单例而发生?
I have a singleton:
public class MySingleton{ public static getInstance(){//typical singleton getInstance ... } //fields private static volatile instance; @Inject private AnotherClassInstanceThatIWantToInjectHere anotherClassInst_BlaBla; private MySingleton(){ ... anotherClassInst_BlaBla.doSmth();//NullPointerException happens! ... } }
What is cause of this NPE? Does it happens because it is constructor or because it is singleton?
原文:https://stackoverflow.com/questions/35508930
更新时间:2023-10-17 14:10
最满意答案
它匹配,因为您的字符串确实包含
[A-Za-z0-9-_. ]
中的一个或多个字符[A-Za-z0-9-_. ]
[A-Za-z0-9-_. ]
设置。 如果您只想这样,请将您的模式更改为:string pattern = "^[A-Za-z0-9-_. ]+$";
它将强制模式从字符串的开头到结尾匹配。
It matches because your string does contain one or more characters in the
[A-Za-z0-9-_. ]
set. If you want only that, change your pattern to this:string pattern = "^[A-Za-z0-9-_. ]+$";
It will force the pattern to match from the beginning to the end of the string.
相关问答
更多-
!使用csharp,Regex.IsMatch无法以窗口形式工作(!Regex.IsMatch is not working in window form using csharp)[2022-11-17]
我无法帮助你处理正则表达式,因为我不是很强大,但我认为你应该在你的if语句中用OR替换AND,以允许编译器在文本框不为空的情况下检查正则表达式。 if (txtRefno.Text.trim() == string.Empty || !Regex.IsMatch(txtRefno.Text, @"^[0-9]+$")) { msg.Text = "reference no. contain Invalid characters"; } 或者如果你不想在texbox为空的情况下显示消息,你可以使用下 ... -
[^0-9]将匹配不是数字的单个字符。 这是你想要的吗? 你确定你不是指\d+而是吗? 你认为它是反向的,因为如果你只测试正数和负数,你将使用的唯一非数字字符将是“ - ”(负号)。 所以它会匹配负号,因为它不是数字。 因此,您发现您的代码反向运行。 例如“-987”(true:匹配负号),“123”(false:未找到非数字字符) 因此,事实是:你的代码不能反向运行。 你的代码错了,做错了。 只是你的输入看起来好像正在反向运行。 [^0-9] will match a single character t ...
-
很明显,你的curSetting.RegExMatch是用RegexOptions.IgnoreCase标志编译的: (?!.*(?:erallog))是不区分大小写的,并且匹配你的1_GeneralLog1370013-170403.log输入字符串中的1_GeneralLog1370013-170403.log ,所以负向前瞻模式找到匹配并且无法完成整体匹配。 所以,有两种方法(取决于你需要的): 从正则表达式对象初始化或中删除RegexOptions.IgnoreCase 将不区分大小写的内联选项(? ...
-
在.NET正则表达式中, $ anchor(如PCRE,Python,PCRE,Perl,但不是 JavaScript)匹配行尾或字符串中最后一行( "\n" )字符前的位置 。 请参阅此文档 : $匹配必须出现在字符串或行的末尾,或者出现在字符串或行末尾的\n之前。 有关更多信息,请参见字符串结尾或行 。 没有修饰符可以在.NET正则表达式中重新定义它(在PCRE中,您可以使用D PCRE_DOLLAR_ENDONLY修饰符)。 你必须要找到\z anchor:它只匹配字符串的最后一部分 : \z匹配必须 ...
-
你可能会滥用变化和捕获组,而你可以将所有这些字符类合并成一个。 你也有*作为量词,这意味着零或更多。 所以无论锚点如何匹配。 你的解决方案是: ^[a-zA-Z0-9 _@.!$-]+$ You are misusing alternations and capturing groups while you could have all those character classes into one. You also have * as quantifier which means zero or m ...
-
因为你用^开始你的正则表达式,并以$结尾。 那些指定匹配必须从字符串的开始处开始并结束于结尾。 因为你的比赛是在中间,所以没有找到。 Because you started your regex with ^ and ended with $. Those specify that the match must begin at the start of the string and end at the end. Since your match is in the middle, it was not ...
-
你可能意味着这个正则表达式: patternMatch = @"^(?i)/(A|B)?(?:/index\.html|/|)$" @是生成正则表达式字符串。 \. 是逃避点,以便匹配文字点(而不是任何字符)。 $是匹配字符串的结尾,并确保正则表达式尝试匹配整个字符串。 You might have meant this regex instead: patternMatch = @"^(?i)/(A|B)?(?:/index\.html|/|)$" @ is to raw the regex stri ...
-
它匹配,因为您的字符串确实包含[A-Za-z0-9-_. ]中的一个或多个字符[A-Za-z0-9-_. ] [A-Za-z0-9-_. ]设置。 如果您只想这样,请将您的模式更改为: string pattern = "^[A-Za-z0-9-_. ]+$"; 它将强制模式从字符串的开头到结尾匹配。 It matches because your string does contain one or more characters in the [A-Za-z0-9-_. ] set. If you w ...
-
方法返回true,即使它不应该返回true。(Method returns true even though it should not. Possible complication with for loop)[2023-09-30]
无论如何都会返回true ,因为你有else阻塞。 因为count从零开始并且每个循环最多增加一次,所以else块将始终运行(如果数组长度不为0)。 您可以将此视为两种情况: 情况1. n == array[i] - > count将等于1 - > count>1 不为 true - > else block运行并返回true。 情况2. n不是array[i] - >计数等于0 - >计数>1 不是真的 - > else块运行并返回true It returns true no matter what b ... -
CollectionProxy通过它们的id比较模型,因为两个记录都是新的并且具有id == nil - include? 返回true 。 要使测试正常工作,必须将模型保存到数据库(使用create!等) I've worked out the solution. I'll document this here in case anyone else has a similar issue. These lines are the problem... subject { described_class. ...