Sign Up

Sign Up to our social questions and Answers to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In


Have an account? Sign In Now

Sign In

Login to our social questions & Answers to ask questions, answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Sorry, you do not have permission to add post.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Oraask Logo Oraask Logo
Sign InSign Up

Oraask

  • Write
    • Add A New Post
    • Ask A Question

Oraask Navigation

Search
Ask A Question

Mobile menu

Close
  • Categories
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Dev Tools
    • Online Compiler
    • Base64 Converter
  • Wiki
    • SQL Tutorials
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
Home/ Questions/Q 24658
Next
In Process

Oraask Latest Questions

Question
Olivier
  • 0
  • 0
OlivierExplorer
Asked: January 6, 20182018-01-06T18:36:35+03:00 2018-01-06T18:36:35+03:00In: Java

How to avoid != null statements in java ?

  • 0
  • 0

I want to address the necessity to test every object if you want to access a field or method of this object.

For example:

[code]
if (someobject != null) {
someobject.doCalc();
}
[/code]

In this case I will avoid a NullPointerException, and I don’t know exactly if the object is null or not. These tests appear throughout my code as a consequence.

Is there a good alternative to this?

javaNULLNullPointerExceptionstatement
1
  • 1 1 Answer
  • 703 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    Related Questions

    • What are the differences between methods and constructors in java ?
    • How can I remove an element from an array in java
    • How can we set java path in windows 10
    • Does StringUtils.isNotEmpty check for null
    • How to avoid deadlock in Java

    1 Answer

    • Voted
    • Oldest
    • Recent
    1. Googler
      Googler Explorer
      2018-02-28T20:51:37+03:00Added an answer on February 28, 2018 at 8:51 pm

      If you use (or planning to use) JetBrains IntelliJ IDEA, a Java IDE, you can use some particular annotations developed by them.

      Basically, you’ve got @Nullable and @NotNull.

      You can use in method and parameters, like this:

      [code]
      @NotNull public static String helloWorld() {
      return “Hello World”;
      }
      [/code]

      or

      [code]
      @Nullable public static String helloWorld() {
      return “Hello World”;
      }
      [/code]

      The second example won’t compile (in IntelliJ IDEA).

      When you use the first helloWorld() function in another piece of code:

      [code]
      public static void main(String[] args)
      {
      String result = helloWorld();
      if(result != null) {
      System.out.println(result);
      }
      }
      [/code]

      Now the IntelliJ IDEA compiler will tell you that the check is useless, since the helloWorld() function won’t return null, ever.

      Using parameter

      [code]
      void someMethod(@NotNull someParameter) { }
      [/code]

      if you write something like:

      [code]
      someMethod(null);
      [/code]

      This won’t compile.

      Last example using @Nullable

      [code]
      @Nullable iWantToDestroyEverything() { return null; }
      [/code]

      Doing this

      [code]
      iWantToDestroyEverything().something();
      [/code]

      And you can be sure that this won’t happen. 🙂

      It’s a nice way to let the compiler check something more than it usually does and to enforce your contracts to be stronger. Unfortunately, it’s not supported by all the compilers.

      In IntelliJ IDEA 10.5 and on, they added support for any other @Nullable @NotNull implementations.

      See blog post More flexible and configurable @Nullable/@NotNull annotations.

        • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    Sidebar

    Adv 250x250

    Explore

    • Categories
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Dev Tools
      • Online Compiler
      • Base64 Converter
    • Wiki
      • SQL Tutorials
      • Java Tutorials
      • Python Tutorials
      • JavaScript Tutorials

    Footer

    Oraask

    About

    Oraask is a website for developers and software engineers who want to learn new skills, share their knowledge, and solve their coding problems. Oraask provides free content on various programming languages and topics, such as Oracle, Python, Java, etc. Oraask also allows users to ask questions and get answers from other members of the community.

    About Us

    • About Us
    • Contact Us

    Legal Stuff

    • Privacy Policy
    • Terms & Conditions

    Follow

    Oraask is licensed under CC BY-NC-SA 4.0Oraask CopyrightOraask CopyrightOraask CopyrightOraask Copyright

    © 2019 Oraask. All Rights Reserved
    With Love by Oraask.

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.