首页 \ 问答 \ 吉斯。(Guice. Inject into constructor)

吉斯。(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.

相关问答

更多
  • 我无法帮助你处理正则表达式,因为我不是很强大,但我认为你应该在你的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 ,因为你有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. ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。