我有一个名为 MyClass 的类,在该类中我正在调用 MyClass.class.getClassLoader().getResource("SCHEMA.xsd")
。当我对此代码运行强化扫描时,它提示我在调用 getResource()
之前在 MyClass.class.getClassLoader()
上处理 null。我真的需要在这里处理 null 吗?什么情况下getClassLoader()
会返回null?
最佳答案
根据 Javadoc
Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader. [...] If this object represents a primitive type or void, null is returned.
如果您不使用任何框架,那么大多数时候您应该是安全的。请参阅文档:https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getClassLoader()
编辑:使用@mangotang的评论进行更正
关于java - 在调用 getResource() 之前,我是否需要在 getClassLoader() 上处理 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42520445/