Issue
Our legacy project is using ClassLoader.getSystemClassLoader().getResource("some resource")
to find out some URL.
Now coverity is throwing errors on ClassLoader.getSystemClassLoader() saying that it can be null.
Now, upon looking at the code of getSystemClassLoader(), it looks like in some scenario it can be null. And it is getting very complicated to understand the reason when it can be null.
My question is if an application has successfully started and is up and running, can still be there any chance that ClassLoader.getSystemClassLoader() can be null. Bcoz this method seems like it is used to start the java application.
can some one give an easy definition of the use of this method. I am not able to fully understand the java docs.
Solution
To get a resource from the system classloader, use getSystemResource(String name)
.
So change the code as follows:
// Before
ClassLoader.getSystemClassLoader().getResource("some resource")
// After
ClassLoader.getSystemResource("some resource")
Answered By - Andreas
Answer Checked By - Timothy Miller (JavaFixing Admin)