Java 从同一个包读取的属性 lang.NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18495252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
properties lang.NullPointerException reading from same package
提问by Thufir
I've tried variations on the path to the properties file, but can't seem to get it correct.
我已经尝试了属性文件路径的变体,但似乎无法使其正确。
Here's the structure:
src/
├── properties.properties
└── teln
├── ConnectMUD.java
├── IOUtil.java
└── PropertiesReader.java
and the error:
和错误:
run:
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at teln.PropertiesReader.getProps(PropertiesReader.java:16)
at teln.ConnectMUD.main(ConnectMUD.java:18)
and the class in question:
和有问题的班级:
package teln;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PropertiesReader {
private static final Logger LOG = Logger.getLogger(PropertiesReader.class.getName());
private static Properties props = new Properties();
public static Properties getProps() {
try {
props.load(PropertiesReader.class.getResourceAsStream("/teln/teln.properties"));
} catch (IOException ex) {
Logger.getLogger(PropertiesReader.class.getName()).log(Level.SEVERE, null, ex);
}
LOG.fine(props.toString());
return props;
}
}
The project is Teln
and the package is teln
(not good choices, maybe).
项目是Teln
,包是teln
(可能不是好的选择)。
采纳答案by Sotirios Delimanolis
Your code and setup don't match. You ask for a teln.properties
file inside teln
but you show a properties.properties
file outside of teln
. Please be consistent in your question.
您的代码和设置不匹配。您要求在teln.properties
里面有一个文件,teln
但properties.properties
在teln
. 请在您的问题中保持一致。
The javadoc for Class#getResource(String)
says it all.
javadoc forClass#getResource(String)
说明了一切。
The ClassLoader
will look at the root of the classpath (you can define the classpath in your Netbeans configuration).
在ClassLoader
将着眼于classpath中(可以定义在NetBeans配置类路径)的根。
With a Netbeans setup like
使用 Netbeans 设置,例如
src/
├── properties.properties
└── teln
├── ConnectMUD.java
├── IOUtil.java
└── PropertiesReader.java
we can assume that the classpath root will be
我们可以假设类路径根将是
properties.properties
teln/ConnectMud.class
teln/IOUtil.class
teln/PropertiesReader.class
If you want to get the properties.properties
resource, you need to get it like so
如果你想得到properties.properties
资源,你需要像这样得到它
props.load(PropertiesReader.class.getResourceAsStream("/properties.properties"));
The prefixed /
means to make the path relative to the root of the classpath. If you had ommited it, the method would have looked at the root of the package the class PropertiesReader
is in.
前缀/
意味着使路径相对于类路径的根。如果您省略了它,该方法将查看类所在的包的根目录PropertiesReader
。
Clue: If you are unsure what your classpath is, use Netbeans to compile a Jar of your project and look in there.
提示:如果您不确定您的类路径是什么,请使用 Netbeans 编译您的项目的 Jar 并在那里查看。
回答by Ankit
Can you try replacing
你可以尝试更换吗
props.load(PropertiesReader.class.getResourceAsStream("/Teln/teln.properties"));
props.load(PropertiesReader.class.getResourceAsStream("/Teln/teln.properties"));
with
和
props.load(PropertiesReader.class.getResourceAsStream("/teln/teln.properties"));
props.load(PropertiesReader.class.getResourceAsStream("/teln/teln.properties"));
回答by Thufir
this works:
这有效:
props.load(PropertiesReader.class.getResourceAsStream("/connection.properties"));
with this structure:
使用这种结构:
thufir@dur:~/NetBeansProjects/Teln$
thufir@dur:~/NetBeansProjects/Teln$ tree src/
src/
├── connection.properties
└── teln
├── ConnectMUD.java
├── IOUtil.java
└── PropertiesReader.java
1 directory, 4 files
thufir@dur:~/NetBeansProjects/Teln$