iamukt iamukt - 1 year ago 420
Java Question

SonarQube Blocker Issue NullPointerException might be thrown as 'contacts' is nullable here

I have a method that returns list of contacts. When I am running this code on SonarQube server. It shows a blocker issue stating NullPointerException might be thrown as 'contacts' is nullable here.. How to resolve this?

List<Contact> getContactDetails(){...}

public void checkSize() {
List<Contact> contacts = getContactDetails();

Answer Source

Depending on your implementation of getContactDetails(), this method might return null, and so the line


might fail due to an NPE because contacts could be null.

Fix this by either adding

if(contacts != null) {
} else {
    // exception, error handling or nothing

or by not returning null in getContactDetails().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download