java - 在调用 getResource() 之前,我是否需要在 getClassLoader() 上处理 null

标签 java

我有一个名为 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/

相关文章:

java - 一般行为

java - 测试装置不会初始化数组

java - com.microsoft.sqlserver.jdbc.SQLServerException : The "variant" data type is not supported

java - 使用 JNI 将 C++ 类成员函数绑定(bind)到 Java

java - 如何从 ISO 3166 代码中查找 ISD 代码?

java - 如何在 Eclipse IDE 中快速验证 Web 应用程序项目?

java - Android 中的动态类加载

java - 从高度调整图像大小

java - JAXP XSLT document()函数问题

java - 如何从.class文件中读取Java类def?