IntelliJ shortcut for inserting a null check?
Is there a shortcut for inserting the code:
if (someParameter == null)
thro开发者_如何学JAVAw NullPointerException("someParameter is null");
Try typing ifn
followed by Tab. You can find more of those or define your own by going to the preferences and searching for "Live Templates".
IntelliJ IDEA provides another functionality, similar to shortcuts, that can help with your productivity, namely, Postfix completion.
If you want to insert a null check, type '.null' after the variable name, e.g.
and the null-checking code will be inserted automatically, e.g.
IntelliJ IDEA provides more handy postfix completions, which you can find in Preferences at Editor > General > Postfix Completion
There isn't a template defined for that but you can easily create one. Just follow the steps described here.
There is now an easy way to do it (in version 13.1), described here: http://blog.jetbrains.com/idea/2014/03/postfix-completion/
If you are okay with having some annotations in your code then you could try the solution mentioned here. It does not solve the problem directly but if you have this kind of null checks at many places you can use this :)
精彩评论