Sign Up

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

Have an account? Sign In

Captcha Click on image to update the captcha.

Have an account? Sign In Now

Sign In

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

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Tech Answered Logo Tech Answered Logo
Sign InSign Up

Tech Answered

Tech Answered Navigation

  • Home
  • Tutorials
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Blog
  • Contact Us
Home / Questions /Q 260
Next
In Process
Aniya
Aniya

Aniya

  • India
  • 157 Questions
  • 89 Answers
  • 3 Best Answers
  • 100 Points
View Profile
  • 0
Aniya
Asked: July 31, 20202020-07-31T11:16:09+05:30 2020-07-31T11:16:09+05:30In: Java

How to solve null pointers should not be dereferenced sonar fix

  • 0

Java Rule ‘Null pointers should not be dereferenced’ and my sonar version is SonarLint IntelliJ 4.0.0.2916

I’m getting the following error java null exception specifically “fault in the next code when obj can’t be null in any scenario. Seems the analizer don’t recognice the logic with the funcional object Optional”.

final Obj obj = Optional.ofNullable(oldObj).orElse(new Obj());

if (obj.getSomeProperty() == null) {

Im using null to validate but why is this error “null pointers should not be dereferenced sonar” can anyone help me fix this error….

fix java errorsfix null pointer exceptionjavajava errorsnull pointer exception javanull pointers should not be dereferenced
  • 10
  • 16,803
  • 0
  • 0
Answer
Share
  • Facebook

    Related Questions

    • Best Programming Language To Learn in 2021?
    • How to resolve the issue java.lang.OutOfMemoryError: Java heap space?
    • java. lang. NullPointerException
    • Which is the best programming language to learn right now?
    • what java is used for ?
    • e java.lang.noclassdeffounderror javax/xml/bind/jaxbexception
    • java equals int cannot be dereferenced how to fix

    10 Answers

    1. Aniya

      Aniya

      • India
      • 157 Questions
      • 89 Answers
      • 3 Best Answers
      • 100 Points
      View Profile
      Aniya
      2020-08-12T09:03:41+05:30Added an answer on August 12, 2020 at 9:03 am
      This answer was edited.

      A Null reference means you are trying to get a non existing element,so java throws it as a exception,which is not really a error, so you will exception like null pointers should not be dereferenced sonar or accessed.

      1. This exception may lead to termination of program execution.So how to fix this in sonar?
      2. you should goto debug log and find @CheckForNull and @Nonnull annotations defined in JSR-305,this helps to check which values are null and which are not. except in @Nonnull case.

       

      I will show you a example program which calls null pointers should not be dereferenced sonar exception.

      WholeClass avgmthd= null;
      …….
      if (!getRatingUtilities().isNR(minRating)) {
      return minRating.getRatgCaam(); //Sonar: Null pointers should not be dereferenced
      }

      as i passed a null value and checking the if condition above,system throwed a null pointer exception ,so find the null accessing areas in your program and try to fix them.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. Pubali Halder

      Pubali Halder

      • Kolkata, India
      • 0 Questions
      • 16 Answers
      • 2 Best Answers
      • 74 Points
      View Profile
      Pubali Halder
      2020-08-16T18:33:50+05:30Added an answer on August 16, 2020 at 6:33 pm

      If you got this kind of error, let me help you out.

      In Java, although we do not always get null pointer errors, sometimes if Null pointer exceptions are thrown when the referenced variable is accessed but is not pointing to any object mentioned in the program code. We can resolve it by try-catch block or if-else conditions. 

      NullPointerException in java is a type of runtime exception. A special null value can be assigned in java to an object reference. 

      1. Why is this error “null pointers should not be dereferenced sonar”?

      If you refer to a null pointer, then it should never be accessed or dereferenced. For this NullPointerException is to be thrown. Such type of exception will cause program termination abruptly.

      It can also expose debugging information at the worst and that can be useful for an attacker to break the security measures. We should also note that when they are present, they can take advantage of both the annotations including @CheckForNull and @Nonnull. Both of them can understand which values are and are not nullable. 

      1. How to solve this error?

      To avoid this error, all the objects should be ensured properly at the time of using them or more appropriately before using them. Also, we should note that when we declare a reference variable, we must verify that the object is not null. To overcome string comparison with literals problem, we should consider invoking it from the literal, instead of invoking the method from the null object. We can also avoid this kind of exception by calling equals literal rather than an object. The ternary operator can also be used to avoid this kind of exception. We should also keep a check on the arguments of a method. 

      If you found this answer useful, don’t forget to like it. Also, share it with people who are getting this error.

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
      • rupesh gote
        2020-11-22T12:18:17+05:30Replied to answer on November 22, 2020 at 12:18 pm

        sounds good, it will be great if you help me with example.
        PrintWriter out = null; try { out = new PrintWriter( new BufferedWriter( new FileWriter(“out.txt”, true))); out.println(“the text”); } catch (IOException e) { e.printStackTrace(); } finally { if (out != null) { out.close(); } }

        what changes should i go with?

        It is some what urgent on my project.

        Thank you

        • 0
        • Reply
        • Share
          Share
          • Share on Facebook
          • Share on Twitter
          • Share on LinkedIn
          • Share on WhatsApp
        • vishal
          2020-11-22T12:31:29+05:30Replied to answer on November 22, 2020 at 12:31 pm

          you got a null pointer exception for the above code execution ??

          • 0
          • Reply
          • Share
            Share
            • Share on Facebook
            • Share on Twitter
            • Share on LinkedIn
            • Share on WhatsApp
          • rupesh gote
            2020-11-24T02:06:06+05:30Replied to answer on November 24, 2020 at 2:06 am

            not null pointer Bt it is shwoing again obj should be closed in finlly. nd if i remove if condition checking Null , it is accepting for “close obj in finally”. then again sonar will pop up Major issue for null pointer.. Please ref SS

             

            Thanks for your time

            How to solve null pointers should not be dereferenced sonar fix
            • 0
            • Reply
            • Share
              Share
              • Share on Facebook
              • Share on Twitter
              • Share on LinkedIn
              • Share on WhatsApp
            • neha

              neha

              • India
              • 1 Question
              • 2 Answers
              • 0 Best Answers
              • 24 Points
              View Profile
              neha
              2021-01-12T22:35:55+05:30Replied to answer on January 12, 2021 at 10:35 pm

              In ss the if statements are two times. Either In the finally block instead of all if statements write out.close() or remove null
              if possible share ss again
              hope it works

              • 0
              • Share
                Share
                • Share on Facebook
                • Share on Twitter
                • Share on LinkedIn
                • Share on WhatsApp
            • Harikrishna
              2021-01-18T15:56:41+05:30Replied to answer on January 18, 2021 at 3:56 pm

              hi Rupesh,

              just make the source with try , for eg:

              Object obj=new Object();———————————(1)

              (1)-any Source.

              then Access the Source with try and use the benefits inside the try block and Catch (if any Exceptions in Catch block)

              ie;

              try(Object obj=new Object()){

              //do the activities with the obj

              }

              catch(Exceptions e)

              {

              //catch -if any exceptions

              }


              the above code will Automatically Close the obj whenever it done its job!!!!!!!

              • 1
              • Share
                Share
                • Share on Facebook
                • Share on Twitter
                • Share on LinkedIn
                • Share on WhatsApp
          • rupesh gote
            2020-11-24T10:08:30+05:30Replied to answer on November 24, 2020 at 10:08 am

            not getting exception, sonar showing major issue on same code

            • 0
            • Reply
            • Share
              Share
              • Share on Facebook
              • Share on Twitter
              • Share on LinkedIn
              • Share on WhatsApp
    3. vrushal Vinod Kumbhare
      2020-08-17T19:36:47+05:30Added an answer on August 17, 2020 at 7:36 pm

      The Java Rule Null Pointer should not be dereferenced.A refrence to null should never be dereferenced.Doing so well cause a Null pointer exception to be the best such exception handling will cause abrupt program termination.This could expose debugging information.

      • -7
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    4. eduardo
      2022-04-01T07:22:23+05:30Added an answer on April 1, 2022 at 7:22 am

      this looks like a bug to me, am posting here what I posted at stackoverflow(https://stackoverflow.com/questions/71697278/sonar-ternary-operation-throws-warning-a-nullpointerexception-could-be-thrown)

       

       

      I have java code like this:

      String str = response != null && response.getBody() != null ? response.getBody().getUser() : null;
      

      Even though, Sonar is showing the following alert:

      A "NullPointerException" could be thrown; "getBody()" can return null.
      

      Clearly this cannot produce NullPointerException; Even worse, if I format this to:

      String str = null;
      if(response != null && response.getBody()) {
         str = response.getBody().getUser()
      }
      

      Sonar shows the exact same warning, therefore, I do not see a possible way to avoid this alert.

       

      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    Leave an answer

    Leave an answer
    Cancel reply

    Browse

    Sidebar

    Ask A Question

    More Questions

    • dynamicapps5

      how to solve error illegal start of expression in java

    • sita

      write a program to calculate the electricity bill in java

    • Himasree

      How to insert data in java through database?

    • Swathi

      How to run java code in eclipse

    • Swathi

      What is null pointer exception in java?

    • kiran kumar85

      javascript validation client side and submitting to database using webspring

    • Rashika

      When to use volatile variable in Java?

    • Aniya

      how to fix java error in tlauncher authentication

    Stats

    • Questions 1,998
    • Answers 2,102
    • Posts 12
    • Best Answers 73

    Related Questions

    • Vipinkv

      Best Programming Language To Learn in 2021?

    • Rashika

      How to resolve the issue java.lang.OutOfMemoryError: Java heap space?

    • Namrata Podder

      java. lang. NullPointerException

    • harrywilson

      Which is the best programming language to learn right now?

    • Pooja Jakhar

      what java is used for ?

    • Mingle tap

      e java.lang.noclassdeffounderror javax/xml/bind/jaxbexception

    • Aniya

      java equals int cannot be dereferenced how to fix

    • Aniya

      java error code 1603 how to fix

    Explore

    • Home
    • Communities
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Polls
    • Tags
    • Badges
    • Users
    • Help

    Footer

    Recent Comments

    • muller on how to solve export problem in kinemaster
    • muller on how to solve export problem in kinemaster
    • muller on how to solve export problem in kinemaster
    • AaronBlackburn on why can’t i find my message requests on instagram
    • shree on How to use philips citrus press juicer 25 watts Demo
    Ask A Question

    Archives

    Tags

    laptop buying guide (1) limit google news notifications (1) printer buying guide (1) stop google news notifications (1) turn off google news notifications (1)

    Social media

    © 2023 @ Techanswered.com.
    Terms, Privacy policy & Sitemap.

    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.