Possible Null Pointer Dereference

Test cases have passed successfully but I’m facing this spotbugs error.
“Possible null pointer dereference in com.crio.warmup.stock.PortfolioManagerApplication.mainReadQuotes(String[]) due to return value of called method”.
List might be null that’s why it’s initialized with an empty list already. I passed milestone 1 by following same procedure but don’t know why it’s failing is milestone 2.

Hey subham! I hope this link will make you understand what actually is triggering the SpotBug: https://stackoverflow.com/questions/12242291/what-is-the-meaning-of-possible-null-pointer-dereference-in-findbug

And here is something that might help you solve it : http://shzhangji.com/blog/2018/09/20/how-to-avoid-null-pointer-exception/

Upvote if useful! Happy coding. :slightly_smiling_face:

1 Like

Basically it means that’s it’s possible that a method you are calling may return null and therefore you should add some form of check before you use the variable in which the method returns the value. Hope that’s helps!

I know what NullPointerException and how to resolve this. But I’m not getting what is causing this error and gradle stacktraces aren’t saying much. Did you guys face similar issue in milestone 2 ??

just add a catch statement regarding this

Adding catch block wasn’t solving it.
I solved it anyways. Thanks guys :slight_smile:

Null pointer exception comes when you declare a variable but not assigned value and use later somewhere in your code. And you are assigning value either in try-catch or in if statement (note not in if-else).

Example.

Employee e;

try {
System.out.println(1/0);
e = new Employee(‘1234’, abcd);
} catch (Exception e) {
e.printstacktrace();
}

System.out.println(e.getName());

In above code I’m using object e outside the try catch so it will give null pointer exception.

Background

If you assign value under try-catch and if and variable declared outside like in above case. There is no guarantee that the variable you’re assigning always assigned to a variable suppose if some code is above the assignment and that assignment fails. Then you you getName() which will definitely give you null pointer exception.

How did you do. some hint please?

It’s related to response you’re getting from api.

check this…it might help you

There is written a line number on which you are these warnings. Please see that line and then try to debug your issue. You will be able to resolve it then.

Null pointer exception doesn’t come from API. It comes when you use possible null pointer and try to dereference it.