Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

Java tuning tutorial 3 - Tuning NPE.RET to reduce false positives

Java tuning tutorial 3 - Tuning NPE.RET to reduce false positives

Let's say we have a snippet:

package com.klocwork.jdefects.checkers.dfa.binding_walkthrough;


public class CheckSample1 {
   public String toString() {   
      final Object o = get();   
      return o.toString();  
   }
 
   private Object get() {   
      if (hashCode() > 0 ) {      
         return new Object();  
      }    
      return null;  
   }
}

An analysis with kwcheck detects the NPE.RET issue, where null comes from the get()and is dereferenced at o.toString().

Add an assertion to fix the error

To fix the NPE.RET issue, add assertNotNull(o); to the code:

package com.klocwork.jdefects.checkers.dfa.binding_walkthrough;


public class CheckSample1 {  
   public String toString() {   
      final Object o = get();
      assertNotNull(o);
      return o.toString(); 
   }
 
   private Object get() {    
      if (hashCode() > 0 ) {    
         return new Object();    
      }  
      return null;  
   }
}

Create a .jkb file and describe the method as the check

After we add the assertion and re-run the analysis, NPE.RET is still reported because the call to assertNotNull is the call to the library method, about which Klocwork does not have any specific information.

  1. Create a text file with the .jkb file extension, for example, check.jkb.
  2. In the file, describe the method as the check:
package junit.framework;


class Assert { 
   public static void assertNotNull(@Check Object object);
}
Note: This step is also captured for Eclipse and IntelliJ IDEA users.

Bind the Check record to the checker

After you add @Check, bind the data to the issue, using @Bind("NPE.RET"):

package junit.framework;


@Bind("NPE.RET")
class Assert { 
   public static void assertNotNull(@Check Object object);

}

Test the knowledge base

To test your knowledge base:

  1. Import the knowledge base into your project using kwcheck:
    kwcheck import check.jkb
    
  2. Run the analysis:
    kwcheck run
    

When the code is analyzed using this knowledge base, Klocwork now knows assertNotNull is the check; consequently, NPE.RET is not reported.