我正面临这个错误:
`Unable to load properties file for MultiWordNet
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:417)
at java.lang.Integer.<init>(Integer.java:660)
at org.itc.mwn.MysqlDictionary.<init>(MysqlDictionary.java:85)`
这是MysqlDictionary.java尝试阅读的属性文件:
#------------------------------------------------------------
#Properties file properties MultiWordNet API
#Hostname of the MySQL server
MWN_HOSTNAME=localhost
#User
MWN_USER=root
#Password
MWN_PASSWD=
#Database name
MWN_DB=wordnet
#Cache of entity
CACHE_CAPACITY=1000
最后,这是代码失败的部分:
public MysqlDictionary() {
try {
connectionParameters = new Properties();
connectionParameters.load(new FileInputStream(new File("./conf/multiwordnet.properties")));
} catch (java.io.IOException ioee) {
System.err.println("Unable to load properties file for MultiWordNet");
}
/// connection drivers instance
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
//Class.forName("org.gjt.mm.mysql.Driver").newInstance();
} catch(ClassNotFoundException E){
System.err.println("Unable to load driver");
} catch(IllegalAccessException E){
System.err.println("Unable to load driver");
} catch(InstantiationException E){
System.err.println("Unable to load driver");
}
// MultiWordnet db connection
String host = connectionParameters.getProperty("MWN_HOSTNAME");
String user = connectionParameters.getProperty("MWN_USER");
String passwd = connectionParameters.getProperty("MWN_PASSWD");
String dbname = connectionParameters.getProperty("MWN_DB");
Integer cache = new Integer(connectionParameters.getProperty("CACHE_CAPACITY"));
//here is where the parsing fails, but the file is properly written!
try {
DEFAULT_CACHE_CAPACITY = cache.intValue();
String conn = "jdbc:mysql://" + host + "/" + dbname;
this.db = DriverManager.getConnection(conn,user,passwd);
this.stmt = db.createStatement();
System.err.println("Welcome to the MultiWordNet API\nConnection database ...OK\n");
} catch (SQLException E) {
System.out.println("Unable to establish multiwordnet Mysql DB connection on " + host + "(" + user + " - " + passwd + ")");
E.printStackTrace(System.out);
}
奇怪的是,程序运行正常后突然失败
答案 0 :(得分:0)
检查<SHIFT>+<SPACE>
或<CTRL>+<SPACE>
之间的不规则隐形字符,或CACHE_CAPACITY=1000
旁边的字符。
听起来有点亵渎,但我经常偶然发现这些事情。
答案 1 :(得分:0)
检查属性文件中的空格或尝试如下
String cacheProp = connectionParameters.getProperty("CACHE_CAPACITY")
//For debug
System.out.println("cacheProp="+cacheProp);
Integer cache = new Integer(cacheProp.trim());
答案 2 :(得分:0)
此代码对我来说很好,我将属性文件保存在资源文件夹(src / main / resources)
中public static void MysqlDictionary() {
try {
Properties p = new Properties();
p.load(MyTest.class.getResourceAsStream("/test.properties"));
String host = p.getProperty("MWN_HOSTNAME");
String user = p.getProperty("MWN_USER");
String passwd = p.getProperty("MWN_PASSWD");
String dbname = p.getProperty("MWN_DB");
String cache = p.getProperty("CACHE_CAPACITY");
int i = Integer.parseInt(cache);
System.out.println(host+"\t"+user+"\t"+passwd+"\t"+dbname+"\t"+i);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
答案 3 :(得分:0)
在java中处理File
时,不要与相对路径斗争。它们非常不可预测,因为它们依赖于您无法完全控制的当前工作目录。请改用classpath,使用类加载器加载资源:
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("multiwordnet.properties"));
您的问题是无法解析整数,但无法加载文件。