Module 2 spotbug error build failure

I facing the following error.

this is the spotbug report

i already visited the FAQ section for spotbugs but not able to fix this issue…
help please @gouravsardana @Kiran @anand-crio

You just need to add a statement which checks if the value returned by the method you are calling is null or not before you use the variable in which the value is returned further.

The error comes because some path of your code is expected to return null in some case… may be you are catchin an exception and reporting null, or anything that results in returning null.

In subsequent clocks, you use the same same variable… so PMD complains that you have chances of facing Null Pointer Exception, from some of the code path.

Checkin your code is not enough, check all of the functions you are calling, and check for Null path in the functions as well.

Lets me know if this doesnt solve your problem.

This error is thrown when we dereference something that might turn out to be null in some cases. A good rule of thumb would be to perform these checks:

  • Before chaining functions like a().b(), save the returned value for a() and then check if it is null
  • Before nesting functions like a(b()), save the returned value for b() and check if it is null

In my file, i’m not able to find any function which is returning null or handling the exception.
and i’m not able to understand “check for Null path…” please elaborate @anand-crio sir

If you click on the warning in html file, it would expand and tell you the line number as well.

@profgrammer While null check can get you rid of some spotbugs, its not a good thing to add.
In programming world, a line of code should not be present if its presence not justified.
i.e. if a variable is not expected to be null, the null check should not be there.

What will happen if you just add null checks, your code will not fail, however it will not do the expected stuff either. So sometimes its recommended that even if we know that the value comes as null in some cases, we do not add null check if its not justified.
Rather, we inspect the code, and look up for the paths that are returning null.

makes sense?

1 Like