12

Is there a shortcut for inserting the code:

if (someParameter == null)
   throw NullPointerException("someParameter is null");
2
  • 2
    I would recommend an IllegalArgumentException instead if this is for checking paramters. Apr 13, 2010 at 9:04
  • 1
    @Thor - Let's not turn this into another discussion - for now, I'm simply interesting in the IntelliJ shortcut, if it exists, not in the type of exception thrown.
    – ripper234
    Apr 13, 2010 at 9:07

5 Answers 5

30

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".

1
  • 1
    The organization of the Live Templates can be confusing. In my installation, there are around 30 categories. The "ifn" template is listed under the "other" category.
    – kc2001
    Jun 25, 2019 at 12:35
15

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.

postfix completion

and the null-checking code will be inserted automatically, e.g.

null checking code

IntelliJ IDEA provides more handy postfix completions, which you can find in Preferences at Editor > General > Postfix Completion

Postfix Completion

3

There isn't a template defined for that but you can easily create one. Just follow the steps described here.

3

There is now an easy way to do it (in version 13.1), described here: http://blog.jetbrains.com/idea/2014/03/postfix-completion/

0

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 :)

1
  • 1
    Link in answer is dead ("Error 404").
    – Pang
    Nov 23, 2016 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.