简体   繁体   English

使用properties.load()时出现NullPointerException

[英]NullPointerException when using properties.load()

I have a little problem with Properties in Java, maybe you could help me. 我在Java中的属性有点问题,也许你可以帮助我。

In the following code i write my Properties object to XML: 在下面的代码中,我将我的Properties对象写入XML:

global.storeToXML(new FileOutputStream(propertiesPath + SEPERATOR + GLOBAL + FILEEXTENSION), "");

This works fine and produces the following file: 这很好,并生成以下文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>test</comment>
<entry key="port">5001</entry>
<entry key="maxPlayers">1</entry>
</properties>

But if i try to load this file with the following code: 但是,如果我尝试使用以下代码加载此文件:

FileInputStream fis = new FileInputStream(propertiesPath + SEPERATOR + GLOBAL + FILEEXTENSION);

if (fis != null)
 global.loadFromXML(fis);

the loadFromXML method throws a NullPointerException. loadFromXML方法抛出NullPointerException。 According to the documentation of Properties this usually means that fis would be null, which it is obviously not. 根据属性的文档,这通常意味着fis将为null,这显然不是。

I already tried the load() and store() methods (without XML) and get just the same error. 我已经尝试了load()和store()方法(没有XML)并得到了同样的错误。 When I try to read the file manually via fis it works fine. 当我尝试通过fis手动读取文件时,它工作正常。

Thank you for you help! 谢谢你的帮助!

Forgot to initialize global. 忘了初始化全局。 Sorry for the dumb question. 抱歉这个愚蠢的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM