在Java中读取属性文件时出现NullPointerException
我使用以下代码来读取属性文件:
Properties pro = new Properties(); InputStream is = Thread.currentThread().getContextClassLoader(). getResourceAsStream("resources.properties"); pro.load(is);
当我执行代码时,我收到以下错误:
Exception in thread "main" java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:418) at java.util.Properties.load0(Properties.java:337) at java.util.Properties.load(Properties.java:325) at com.ibm.rqm.integration.RQMUrlUtility.RQMRestClient.getResource(RQMRestClient.java:66) at com.ibm.rqm.integration.RQMUrlUtility.RQMRestClient.main(RQMRestClient.java:50)
为什么我得到NullPointerException
? 我应该在哪里保存resources.properties
文件?
看起来ClassLoader.getResourceAsStream(String name)
返回null
,然后导致Properties.load
抛出NullPointerException
。
以下是文档的摘录:
URL getResource(String name)
:查找具有给定名称的资源。 资源是一些数据(图像,音频,文本等),可以通过类代码以独立于代码位置的方式访问。资源的名称是标识资源的
'/'
分隔路径名。返回:用于读取资源的
URL
对象,如果是,则返回null
:
- 无法找到资源,或
- 调用者没有足够的权限来获取资源。
也可以看看
- Java教程/使用
getResource
加载图像- 有关于在目录/ JAR文件中放置和访问资源的位置/方式的示例
- SDN:与资源无关的位置访问
如果你写更多行,错误修正会更容易,例如:
Properties properties = new Properties(); Thread currentThread = Thread.currentThread(); ClassLoader contextClassLoader = currentThread.getContextClassLoader(); InputStream propertiesStream = contextClassLoader.getResourceAsStream("resource.properties"); if (propertiesStream != null) { properties.load(propertiesStream); // TODO close the stream } else { // Properties file not found! }
我有同样的问题,因为我以前在Sturts应用程序中使用它时非常困惑。 但问题是我不理解Struts返回的ClassLoader类型与Spring返回的类型不同。 我想出来的方式是我打印出返回到系统控制台的对象,如下所示:
System.out.println(Thread.currentThread().getContextClassLoader());
[
WebappClassLoader
context:/ MyProject
代表:假
库:
/ WEB-INF /类/
———->父类加载器:
org.apache.catalina.loader.StandardClassLoader@1004901
]
它给了我对象的细节,并且我发现它的类型是WebAppClassLoader ,它将在构建完成后开始在WEB-INF / classes /文件夹中查找文件。 所以我进入该文件夹并查找我的文件所在的位置,因此我给出了相应的路径。
就我而言,它位于/WEB-INF/classes/META-INF/spring/filename.extension
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("META-INF/spring/filename.extension");
瞧!
这解决了这一切。
这得看情况; 根据javadoc … 上下文ClassLoader由线程的创建者提供,供加载类和资源时在此线程中运行的代码使用。 如果未设置,则默认为父线程的ClassLoader上下文。 原始线程的上下文ClassLoader通常设置为用于加载应用程序的类加载器…
因此,如果Thread.currentThread().getContextClassLoader()
在main()函数中并且您还没有创建任何线程,那么它应该与包含方法main的类具有相同的包。 否则它应该出现在创建线程的类中….
我遇到了同样的问题,我通过以下方式解决了问题
-
File file = new File("resources.properties");
-
System.out.println(file.getAbsolutePath());
然后将“resources.properties”文件放在该路径下。
我有同样的问题,这对我有所帮助:
InputStream is; try { is = this.getClass().getClassLoader().getResourceAsStream("config.properties"); prop.load(is); String url = prop.getProperty("url"); String user = prop.getProperty("user"); String pass = prop.getProperty("password"); is.close(); // opening database connection to MySQL server con = DriverManager.getConnection(url, user, pass); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }
在我的场景中,我在这段代码中得到了NullPointerException
LogManager.getLogManager() .readConfiguration(MyClass.class.getResourceAsStream("config/logging.properties"));
我变了
LogManager.getLogManager().readConfiguration(AzLotteryTerm.class.getClassLoader().getResourceAsStream("config/logging.properties"));
现在工作正常!
我遇到了第三方程序的问题,原来我需要包括.
在类路径中,以便程序可以读取当前工作目录中的本地属性文件。
许多人似乎有这个问题,像我一样,他们会在一段时间后放弃。 这是我必须得到的工作。 这里使用相对路径进行文件查找的技巧是确保您的classes文件夹包含资源文件和src文件。 这就是我最终做的事情。
1)如果您正在使用eclipse,请确保存在正确的.classpath设置,并执行PROJECT CLEAN以查看在/ classes下生成的资源文件。 请注意下面的资源文件的classpath-entries位于src / main / resource下
2)如果您正在使用maven,请确保按照https://maven.apache.org/guides/introduction/introduction-to-the-pom.html配置pom.xml并执行mvn clean install以查看目标/类下的文件
3)一旦你获得了/ classes下的资源文件,下面要做的事情就是以下内容。 不要忘记有正斜杠。
try { properties.load(getClass().getResourceAsStream("/mail-config.properties")); } catch (IOException e) { e.printStackTrace(); }
我可以添加一些图像,但没有积分。 🙂
也许,我把所有的头发都拿出来然后我找到了解决方案:
Properties dbParamProperties = new Properties(); InputStream input = null; try { String pathOfAbsolute = this.getClass().getProtectionDomain().getCodeSource().getLocation().toString(); String propertiesFilePath = pathOfAbsolute+"/properties/conf.properties"; propertiesFilePath = propertiesFilePath.replace("file:/", "").replace("/", "\\"); System.out.println(pathOfAbsolute); System.out.println(propertiesFilePath); Paths.get(new URI(pathOfAbsolute)); input = ClassLoader.getSystemResourceAsStream(propertiesFilePath); input = new FileInputStream(propertiesFilePath); dbParamProperties.load( input ); dbUID = dbParamProperties.getProperty("userName"); dbURL = dbParamProperties.getProperty("hosturl"); dbPWD = dbParamProperties.getProperty("password"); dbPort = dbParamProperties.getProperty("port"); dbSID = dbParamProperties.getProperty("servicenameorsid"); } catch (IOException e) { e.printStackTrace(); } catch(Exception ex){ ex.printStackTrace(); }